Use the original session

Source: Internet
Author: User
A long-expected PHP4.0 feature is session support. In contrast, PHP3.0 users have to use a third-party library or cannot implement this function at all. The lack of session support is the most popular criticism of PHP. However, since the release of the early beta version, session support has become part of SyntaxHighlighter. all () in PHP4.0 ();

A long-awaited PHP 4.0 Feature is session support. In contrast, PHP 3.0 users have to use a third-party library or cannot implement this function at all. The lack of session support is the most popular criticism of PHP. However, since the early release of the beta version, session support has become part of PHP 4.0, so this problem has been eliminated. You can maintain user-specific variables during user access to the website through sessions without setting multiple cookies or using hidden form fields, or store information in a database that you may frequently connect. Starting a session on a page means that the PHP engine either needs to start a session (if not started before) or continue the current session: session_start (); when a session is started, an authentication string (for example, 940f8b05a40dda-9c030c9c7745aead9) is sent to the user through a cookie. on the server side, a temporary file with the same name is created, such as a token. This file contains the registration session variables and their values. The most common example of displaying session actions is access counters: start your PHP module and make sure that the PHP code is the first line of the File: No blank, no HTML output, and other code. The reason is that when the session function sends a file header, for example, if blank or HTML output is sent before the session_start () function is called, the system will fail. Youve been here $ count times.
"; The entire access counter code is as follows:
Youve been here $ count times.
";?> If you re-load the above script, you can find that the count value increases. This is indeed exciting. You can also register arrays in sessions. Suppose you already have an array named $ faves: $ faves = array (chocolate, coffee, beer, linux); you can register this array like other single variables: session_register (faves ); the application array is the same as other independent variables of the application, such as $ faves. If you want to show your hobbies on a Web site page, you can register your hobbies in the $ faves session variable and output these values on the other page: "; While (list (, $ v) = each ($ faves) {echo"

  • $ V ";} echo"";?> In this way, you can get a simple and beautiful list that shows your interests. The session variable value cannot be overwritten by the query string. that is to say, you cannot enter http: // www.yourdomain.com/yourscript.php? Count = 56 to assign a new value to the registered session variable $ count. This is critical to security: you can only modify or delete (unregistered) session variables on the server side through scripts. If you want to delete a session variable completely, you should cancel the registration from the system: session_unregister (count); to delete a session completely, if you press the Logout button, the following script is relatively simple: session_destroy (); using sessions to store variable values reduces the database connection load and avoids the compilation of nightmare-like complex code, it also uses a large number of privacy statements to explain why up to 50 cookies are sent to users during access. Now, we only need a cookie and a variable. for example, the same water Pearl reflects the whole world. it is no simpler than this!
  • 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.