Dialogue on the couch (III.)

Source: Internet
Author: User
Tags connect session id mysql mysql database

Market Games

Now that you know the basics, make it a little more complicated and demonstrate a real-life session application. Let's assume that you have a financial portal that allows its users to select four stocks and then show the current market price for each stock and display the user's process on the site on each page.

In the following example, we assume that the user has been authenticated and logged on to the site. We use the MySQL database, which has a table named User_info, for storing four stocks and unique user names for the user. Once a session is initialized, we register the variable to save the username and four shares, then connect to the database, retrieve the value and display it on the page.

The code is as follows:

<?php

Initializes a session session_start ();

Register Session variable

User name
Session_register (' username ');

Selected stock variables
Session_register (' Stock1 ');
Session_register (' Stock2 ');
Session_register (' Stock3 ');
Session_register (' Stock4 ');

Connect MySQL
$db = mysql_connect ("someserver.com", "Tom", "Jones");

Select Database
mysql_select_db ("stock_db", $db);

Querying a database using SQL
$query = "Select Stock_pref1,stock_pref2,stock_pref3,stock_pref4
From User_info where username= ' $username ';

$result = mysql_query ($query, $db);

Take stock code from database and assign value to session variable
List ($stock 1, $stock 2, $stock 3, $stock 4) = Mysql_fetch_row ($result);

echo "Hi $username!<br>";
echo "Your selected stocks are:<br>";
echo "$stock 1<br>";
echo "$stock 2<br>";
echo "$stock 3<br>";
echo "$stock 4<br>";

Code to generate rest of page

?>

PHP4 has many functions associated with the session--most of which don't need to be explained, and they're listed below.

Session_destroy (): Releasing all session data (when a user logs off from a site is useful, you need to release all variables created during his visit).

Session_name (): Sets or reads the name of the current session.

SESSION_ID (): Sets or reads the ID value of the current session.

Session_unregister (session_variable_name): Log off a variable from a special session.

Session_is_registered (): Check if a session variable is already registered.

For example:

<?php

Session_Start ();

if (session_is_registered (username))
{
echo "A session variable by" name \ "Username\"
already exists ";
}
Else
{
echo "No variable named \ username\" registered yet.
Registering ... ";
Session_register (username);
}

?>

Session_encode () and Session_decode (): Encodes the session data into a string or decodes the string into session data.

Here you can use them:

<?php

Session_Start ();

Session_register (' somestring ');
$someString = "I hate cats!";

To weave all the session variables into a string
$SESSSTR = Session_encode ();

can see here
Echo $sessStr;

echo "<br><br>";

Replace with dogs where there is cats
$SESSSTR = ereg_replace ("Cats", "dogs", $SESSSTR);

The session variable was updated after decoding
Session_decode ($SESSSTR);

Show once more $sessstr
Echo $someString;

?>

Finally, before starting Phplib, there is a technical problem you should know--all of the examples above use cookies to save the session ID value on the client. But what if the user's browser is set to reject cookies?

If this happens, you need to pass a session ID from one page to another page embedded in the URL. For example: <a href= "Http://www.someserver.com/admin/preferences.php3?PHPSESSID=&lt"? echo "$PHPSESSID";?> ">edit Your portfolio! </a>

This ensures that the session variable is valid on the subsequent page.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.