8 Tips for PHP scripting (2) Session Usage _php Tutorial

Source: Internet
Author: User
Session usage
  
PHP 4.0 has a feature that has always been expected, which is the session support for PHP. In contrast, PHP 3.0 users have to use a third-party library or do not have this feature at all. Lack of conversational support is one of the biggest flaws in PHP, and it's the most criticized place. However, as session support began as a part of the latter from the early beta version of PHP 4.0, this barrier was lost.
With session support, you can maintain user-specific variables during a user's visit to a Web site without having to set up multiple cookies, use hidden form fields, or store information in a database that you might often want to link to.
Starting a session on a page tells the PHP engine: either you want to start a session (if you didn't) or continue with your current session:
Session_Start ();
Starting a session sends an identity string (such as 940F8B05A40D5119C030C9C7745AEAD9) to the user via a cookie, and a temporary file that matches this is created on the server side, and in the example above, the name looks like this: Sess_ 940f8b05a40d5119c030c9c7745aead9. The file contains the registered session variables and their assignments.
User access counters are the most common instances of using sessions:
Launch your PHP module to ensure that the PHP code is the first line of the file: No whitespace, no HTML output, and so on. This is because when the session function emits a file header, if you send a blank or HTML code before the Session_Start () function, the system will error.
If a session does not yet exist for this user, start one
Session_Start ();
Next, register a variable named count.
Session_register (count);
Registering a variable equals telling PHP that a variable named count is present as long as the session exists. Currently, this variable is not assigned a value. However, if you add a 1 operation to it, the value can be assigned a value of 1:
$count + +;
Consider the lines above, in effect you have started a session (if not previously), assigned a session ID to a user, registered a variable named count, and added $count 1 to indicate the user's first access page:

To display the number of times a user accesses a page under the current session, you can simply print out the $count value:
echo "
Youve been here $count times.
";
The entire access counter code is as follows:
Session_Start ();
Session_register (count);
$count + +;
echo "
Youve been here $count times.
";
?>
If you reload the above script, you can observe that the count value is increased. Funny, huh?
You can also register an array in a session. Suppose you have an array named $faves:
$faves = Array (chocolate,coffee,beer,linux);
You can register the array like any other single variable:
Session_register (faves);
There is no difference between indexed arrays and other univariate variables, such as $faves. If your users want to show their hobbies on a page in a Web site, then you can simply register what he likes as a session variable called $faves, and then you can print these values on other pages:
Session_Start ();
echo "My user likes:
      ";
      while (list (, $v) = each ($faves)) {
      echo "
    • $v "; }
      echo "
";
?>
This is what you get: A nice list of users ' hobbies.
A session variable cannot be overridden by a query string, which means that you cannot type an instruction such as http:///www.yourdomain.com/yourscript.php?count=56 to assign a new value to the registered session variable $count. This is important for security: You can only modify or delete (unregistered) session variables on server-side scripts.
If you want to completely delete a session variable, you can unregister the variable from the system:
Session_unregister (count);
To completely delete a session, such as by pressing the logout button, you can write the following code:
Session_destroy ();
Using sessions to store variable values allows us to avoid the pain of writing database processing code, so that it does not unduly increase the load on the system, but also reduces the use of proprietary database syntax, and you no longer have to send a lot of cookies to users who visit the site. And now--just need a cookie, a variable is all done, really a drop of water reflected all the glory! It can't be easier than that.

http://www.bkjia.com/PHPjc/532278.html www.bkjia.com true http://www.bkjia.com/PHPjc/532278.html techarticle Session Usage PHP 4.0 has a feature that has always been expected, which is the session support for PHP. In contrast, PHP 3.0 users have to use third-party libraries or complete ...

  • 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.