Home > Article > Backend Development > Conversation on the couch (4)_PHP tutorial
There are many ways to change the skin of cats...
Of course, if your site is still running PHP3, you won’t be able to use any of the code so far. But don't despair - there is another solution available to PHP3 users. It's called PHPLIB, and it provides a set of useful classes that allow you to add session management to your PHP3. You can install it by following the instructions in the package. You will also need to modify the "local.inc" configuration file to create your own classes.
Like in PHP4, every time you initialize a session, you need to call the page_open() function in advance. PHPLIB returns a default class named Example_Session -- you can modify this value in the "local.inc" file -- which will be used in the following example:
$#@60;?php
page_open(array("sess" =$#@62; "Example_Session"));
?$#@62;
It is important to call page_open() before any output to the browser. In order to register your session variables, the following statement will do the job.
$#@60;?php
//Initialize a session page_open(array("sess" =$#@62; "Example_Session"));
//Register one session variable $sess-$#@62;register(username);
?$#@62;
Each page must also contain the corresponding page_close() function to confirm that all changes are saved in the database.
$#@60;?php
page_close();
?$#@62;
Apart from these, most of the code is the same. Take a look at the PHPLIB version of the previous example:
$#@60;?php
//Initialize a session page_open(array("sess" =$#@62; "Custom_Session"));
/ /Register session variables - note the syntax
$sess-$#@62;register(username);
$sess-$#@62;register(stock1);
$sess-$#@62; register(stock2);
$sess-$#@62;register(stock3);
$sess-$#@62;register(stock4);
//Connect to MySQL $db = mysql_connect("someserver.com", "tom", "jones");
//Select the database mysql_select_db("stock_db",$db);
//Use SQL to query the database
$query = "select stock_pref1,stock_pref2,stock_pref3,stock_pref4
from user_info where username=$username";
$result = mysql_query($query,$db);
// Get the stock code from the database and assign it to the session variable
list($stock1,$stock2,$stock3,$stock4) = mysql_fetch_row($result);
//Output
echo "Hi $usernam #@60;br$#@62;";
echo "Your selected stocks are:$#@60;br$#@62;";
echo "$stock1$#@ 60;br$#@62;";
echo "$stock2$#@60;br$#@62;";
echo "$stock3$#@60;br$#@62;";
echo "$stock4$#@60;br$#@62;";
//Generate the rest of the page code
//Save the data to the database
page_close();
?$#@62;
As you can see, once you put down the PHP4 version, it is not difficult to understand the PHPLIB version - it will not be surprising when you know that the internal support for sessions in PHP4 is largely based on the PHPLIB module. If you are interested, PHPLIB actually goes deeper into identity authentication and permission classes in session management. It allows you to give a user the power to allow or prohibit processing in database-based authentication management - you can take a look. Examples of how to use these features are included in the documentation.
PHPLIB also provides some interesting built-in functions.
unregister(variable)
Allows you to unregister a variable from a particular 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.
$#@60;?php
page_open(array("sess" =$#@62; "Example_Session"));
//Register a variable $sess-$ #@62;register(username);
//Check if it has been registered
if($sess-$#@62;is_registered(username))
{
echo "Variable "username" is registered!$#@60;br$#@62;";
}
else
{
echo "Variable "username" is unregistered!$#@60; br$#@62;";
}
//Unregister a variable $sess-$#@62;unregister(username);
//Check whether it has been logged out if ($sess-$#@62;is_registered(username))
{
echo "Variable "username" is registered!$#@60;br$#@62;";
}
else
{
echo "Variable "username" is unregistered!$#@60;br$#@62;";
}
page_close();
?$#@62;
is_registered(variable) If a session has been registered, return true, otherwise return false.
$#@60;?php
page_open(array("sess" =$#@62; "Example_Session"));
if($sess-$#@62 ;is_registered(username))
{
echo "A session variable by the name "username" already
exists";
}
else
{
$sess-$ #@62;register(username);
}
page_close();
?$#@62;
delete() releases the current session.
An interesting point to note is that in the cookie mode of PHPLIB, it is possible to start a new session after calling delete(), set a new cookie on the client, or even re-register some previous cookies. Session variables - can make changing the session virtually on the fly. Of course, if you do things like this, you need to get your life going...fast!
url($url) allows you to redirect the user to a new page.
self_url() returns a reference to the URL of the current page, including PHP_SELF and QUERY_STRING information.
So finally, for those of you who are unfortunate enough to have an ISP that does not provide PHP4 and PHPLIB - remember, it is always possible to simulate a session with good cookie technology. All you have to do is set a cookie with information to retain the user's visit to your site for a long time, and process this information each time the user visits a new page. It's primitive -- but it works, and sometimes you can't kill a simple way of doing something!
The patient leaves the hospital
Patient: Wow, doctor -- that's great! Thank you so much for everything!
Psychiatrist: No problem, Victor. I'd be happy to help you. Feeling better now?
Patient: Oh, easy! When I first came in, the whole sky looked gray and gloomy -- now, the view outside this awning window has never been better...
Psychiatrist: Well... Victor ...if I were you I would be very careful out there, the handrails are a little broken and may not be safe there.
Patient: Don't worry, doctor -- on days like this, I feel divine...ahah! ! ! !