There are many ways to skin a cat ...
Of course, if your site is still running PHP3, you won't be able to use any code to date. But don't be discouraged-there is another solution available to PHP3 users. It's called Phplib, which provides a useful set of classes that allow you to increase the management of sessions in your PHP3. You can follow the instructions in the package and you will also need to modify the "local.inc" configuration file to create your own class.
As in PHP4, you need to call the Page_open () function in advance each time you initialize a session. Phplib returns a default class named Example_session--You can modify the value in the "local.inc" file--it will be used in the following example:
<?php
Page_open (Array ("Sess" => "example_session"));
?>
It is important to call Page_open () before any output sent to the browser. To register your session variable, the following statement can do the job.
<?php
Initializes a session Page_open (array ("Sess" => "example_session"));
Register a Session variable $sess->register (' username ');
?>
Each page must also contain the corresponding page_close () function to confirm that the amount of change is saved in the database.
<?php
Page_close ();
?>
Except for this, most of the code is the same. Take a look at the Phplib version of the previous example:
<?php
Initializes a session Page_open (array ("Sess" => "custom_session"));
Register Session variable-NOTE syntax
$sess->register (' username ');
$sess->register (' Stock1 ');
$sess->register (' Stock2 ');
$sess->register (' Stock3 ');
$sess->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);
Output
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>";
Generate the rest of the page code
Saving data to a database
Page_close ();
?>
As you can see, once you put down the PHP4 version, it's not difficult to understand the Phplib version-you wouldn't be surprised to learn that the internal support for the session in PHP4 is largely based on the Phplib module. If you are interested, phplib actually in the management of the session, the identity and permissions of the class more in-depth, it allows you in the database based on authentication management, to a user to allow or prohibit the processing of power-can look at the document on how to use these features examples.
Phplib also provides some interesting builtin functions.
Unregister (variable)
Allows you to unregister a variable from a special session. Note that in that case, the variable is not deleted, but its value will be lost at the end of the page because it is no longer saved to the database.
<?php
Page_open (Array ("Sess" => "example_session"));
Register a variable $sess->register (' username ');
Check if it's already registered.
if ($sess->is_registered (' username '))
{
echo "Variable \" username\ "is registered!<br>";
}
Else
{
echo "Variable \" username\ "is unregistered!<br>";
}
Unregister a variable $sess->unregister (' username ');
Check if ($sess->is_registered (' username ') has been logged off
{
echo "Variable \" username\ "is registered!<br>";
}
Else
{
echo "Variable \" username\ "is unregistered!<br>";
}
Page_close ();
?>
is_registered (variable) returns TRUE if a session has been registered, otherwise returns false.
<?php
Page_open (Array ("Sess" => "example_session"));
if ($sess->is_registered (' username '))
{
echo "A session variable by" name \ "Username\" already
Exists ";
}
Else
{
$sess->register (' username ');
}
Page_close ();
?>
Delete () releases the current session.
It is interesting to note that in Phplib cookie mode, it is possible to start a new session after a call to delete (), set a new cookie on the client, or even re-register some of the previous session variables- Can fundamentally change the session like flying. Of course, you do things like this, you need to make your life ... Get up!
The URL ($url) allows you to redirect users to a new page.
Self_url () returns a reference to the URL of the current page, including php_self and query_string information.
So in the end, for those of you who unfortunately have an ISP that doesn't offer PHP4 and Phplib, remember that it's always possible to simulate a session with a good cookie technology. All you have to do is set up a cookie with information to keep the user's access to your site for long periods of time, and to process the information whenever the user accesses a new page. Very primitive--but it works, and sometimes you can't kill a simple way to do something!
The patient left the hospital.
Patient: Wow, doctor-great! Thank you so much for everything!
Shrink: No problem, Victor. I'd be happy to help you. Do you feel better now?
Patient: Oh, easy! When I first came in, the whole day looked grey and gloomy--now, the scenery outside this awning window has never been better ...
Psychiatrist: Well ... Victor... If I were you on the outside would be very careful, the handrail is a bit broken, in that may not be very safe.
Patient: Don't worry, doctor--like this day, I feel God ... St... Ah,!!!!.