Phpsession use detailed instance code

Source: Internet
Author: User
Session_start (): enable the session mechanism. call session_register (): register the session variable session_unregister () at the beginning of the program file that requires the session ():

Session_start (): Enables the session mechanism and calls it at the very beginning of the program file that requires the session.

Session_register (): registers the session variable.

Session_unregister (): deletes session variables one by one)

Session_is_registered (): determines whether the session variable is registered.

Session_distroy (): destroys all session variables (destroys all session variables)

Initialize session, instance 1:

  1. Session_start ();
  2. // Delete all session variable values
  3. $ _ Session = array ();
  4. If (isset ($ _ cookie [session_name ()])
  5. {
  6. Setcookie (session_name (), '', time ()-42000 ,'/');
  7. }
  8. // Cancel all sessions
  9. Session_destroy ();

Instance 2

  1. Session_start (); // initialize the session
  2. $ A = 5; // define the variable
  3. Session_register ('A'); // register the session
  4. Session_decode (" "); // Decrypt the string
  5. Print (session_is_registered ('A ')? $ _ Session [a]: 'Not registered'); // determines whether the variable is registered.

Instance 3

  1. /* Initialize the session */
  2. Session_start ();
  3. $ _ Session ['login _ OK '] = true; // defines the session value.
  4. $ _ Session ['nome'] = 'sica '; // defines the session value.
  5. $ _ Session ['inteiro '] = 34; // defines the session value.
  6. $ Result = session_encode (); // encrypt the session value
  7. Echo $ result; // output result
  8. Session_destroy (); // deregister all session values

Instance 4

  1. Session_start (); // initialize the session
  2. $ _ Session ['name'] = "yoursession"; // register a session variable
  3. If (! Isset ($ _ session ['initiation']) // if a value is not set
  4. {
  5. Session_regenerate_id (); // Obtain the current session id
  6. $ _ Session ['initiation'] = true; // The set value is true.
  7. }
  8. Session_destroy (); // cancel the session

Instance 5

  1. Session_start (); // initialize the session
  2. $ _ Session ['name'] = "yoursession"; // register a session variable
  3. If (session_is_registered (name) // judge
  4. {
  5. Echo "the specified variable has been registered as a session
    "; // Output Content
  6. }
  7. Else
  8. {
  9. Echo "the specified variable is not registered as a session
    "; // Output the corresponding result
  10. }
  11. Session_destroy (); // cancel the session

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.