8 tips for PHP scripts (2) _ PHP Tutorial

Source: Internet
Author: User
8 tips for PHP scripts (2 ). Session usage PHP4.0 has a feature that has always been expected, which is supported by PHP sessions. In contrast, PHP3.0 users have to use third-party libraries or full sessions.
  
PHP 4.0 has an always-expected feature, which is supported by PHP sessions. In contrast, PHP 3.0 users have to use third-party libraries or do not have the functionality at all. Lack of session support is one of the biggest defects of PHP, and is also the most widely recognized part. However, as session support has become part of the latter since the early version of PHP 4.0, this obstacle also disappears.
With session support, you can maintain user-specific variables during user access to the network site without having: set multiple cookies, use hidden form fields, or store information in a database that you may frequently connect.
Starting a session on a page tells the PHP engine that you want to start a session (if not) or continue the current session:
Session_start ();
When a session is started, an identifier string (such as 940f8b05a40d3169c030c9c7745aead9) is sent to the user through a cookie. a temporary file that matches this is created on the server. in the preceding example, the name is like this: sess_940f8b05a40d3169c030c9c7745aead9. This file contains the registered session variables and their assigned values.
User access counters are the most common examples of using sessions:
Start your PHP module to ensure that the PHP code is the first line of the File: No blank, no HTML output, and so on. This is because when the session function sends a file header, if you send blank or HTML code before the session_start () function, the system reports an 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 tells PHP that, as long as the session exists, a variable named count also exists. Currently, this variable has not been assigned a value. However, if you add 1 to it, the value is assigned to 1:
$ Count ++;
Considering the above code, you have actually started a session (if not) assigned a session id to a user, registered a variable named count, and added $ count to 1 to indicate that the user accessed the page for the first time:
To display the number of times a user accesses the page under the current session, you only need to print the value of $ count:
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 preceding script, you can see that the count value has increased. Interesting?
You can also register arrays in sessions. Suppose you have an array named $ faves:
$ Faves = array (chocolate, coffee, beer, linux );
You can register the array like other single variables:
Session_register (faves );
There is no difference between the index array and other single variables, such as $ faves. If your users want to show their hobbies on a Web site page, you can register what they like as a session variable named $ faves, 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 need: a beautiful list of user hobbies.
Session variables cannot be overwritten by query strings. that is to say, you cannot type http: // www.yourdomain.com/yourscript.php? A command like count = 56 assigns a new value to the registered session variable $ count. This is very important for security: you can only modify or delete (unregistered) session variables on the server script.
If you want to completely delete a session variable, you can cancel the registration from the system:
Session_unregister (count );
Permanently delete a session. for example, if you press the Logout button, you can write the following code:
Session_destroy ();
Using sessions to store variable values saves us from the pain of writing database processing code. this will not overload the system, but also reduce the use scope of the proprietary database syntax, besides, you no longer have to send a large number of cookies to users who access the website. Now, we can get everything done with just one cookie and one variable. a drop of water shows all the glory! It cannot be simpler.


Parse PHP 4.0 has an always-expected feature, which is supported by PHP sessions. In contrast, PHP 3.0 users have to use third-party libraries or completely...

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.