Using the original session _php

Source: Internet
Author: User
A PHP 4.0 feature that is expected by everyone for a long time is session support. In contrast, PHP 3.0 users have to use a third-party library or simply do not implement this feature. The lack of conversational support is one of the most critical areas of PHP. However, since the release of the early beta release, session support became part of PHP 4.0, and the problem was eliminated.



You can use a session to maintain user-specific variables during a user's visit to a network site, without having to set up multiple cookies, using hidden form fields, or storing information in a database that you may often want to link to.

Starting a session on a page tells the PHP engine either to start a session (if it didn't start before) or to continue the current session:

Session_Start ();

Starting a session sends an authentication string (such as 940F8B05A40D5119C030C9C7745AEAD9) to the user via a cookie, while on the server side creates a matched temporary file with the same name, such as Sess_ 940f8b05a40d5119c030c9c7745aead9. This file contains the registered session variables and their values.

The most common example of displaying session actions is to access counters:

Start your PHP module and make sure that the PHP code is the first line of the file: No whitespace, no HTML output, and any code. The reason is that when a session function emits a file header, such as a blank or HTML output is sent before the Session_Start () function is called, the system will get an error.


Start a new session if the session does not exist for the user

Session_Start ();

Next, register the count variable.

Session_register (' count ');

Registering a variable is the PHP: As long as the session exists, a variable called count also exists. Currently, this variable has not been assigned a value. However, if you add 1 to it, the value can be assigned to 1:

$count + +;
Consider the above points, you have done the following: A session was started (if not previously), a user was assigned a session ID (if there is no session ID), a variable named count was registered, and $count was added 1 to indicate the first time the user visited the page:



To display the number of times a user has visited a page in the current session, you only need to output the $count value:

echo "

You ' ve been here $count times.

";

The entire access counter code is as follows:


Session_Start ();

Session_register (' count ');

$count + +;

echo "

You ' ve been here $count times.

";

?>

If you reload the above script, you can find that the count value increases. It's really exciting.

You can also register an array in a session. Suppose you already have an array named $faves:

$faves = Array (' chocolate ', ' coffee ', ' beer ', ' Linux ');

You can register the array like any other single variable:

Session_register (' faves ');

The application array is the same as applying other individual variables, such as $faves. If a user wants to show their hobby live on a page in a Web site, they can register the user's hobby in the name $faves session variable and output the values on another page:


Session_Start ();

echo "My user likes:

      ";

      while (list (, $v) = each ($faves)) {

      echo "
    • $v "; }

      echo "
";

?>

This gives you a beautiful and concise list of users ' hobbies.

The session variable value cannot be overridden by the query string, that is, you cannot assign a new value to the registered session variable $count by typing http:///www.yourdomain.com/yourscript.php?count=56. This is critical for security: You can only modify or delete (unregistered) session variables on the server side by script.

If you want to completely delete a session variable, you should unregister the variable from the system:

Session_unregister (' count ');

To completely delete a session, such as pressing the logout button, the following script is more concise:

Session_destroy ();

Using sessions to store variable values eases database connection load, avoids the writing of Nightmare Complex code, and uses a number of privacy statements to explain why up to 50 cookies are sent to users during the visit. And now you just need a cookie, a variable--like a drop of water that reflects the world--nothing is easier!
  • 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.