Use session in php

Source: Internet
Author: User
Use session in php to create sessionsession_start (); $ _ SESSION ['username'] = $ username ;? On the page to be verified, use // to verify whether session exists in username php.

Create a session on the logon page

session_start(); $_SESSION['username'] = $username;

? Use

// Verify sessionsession_start (); // check whether username exists in the session and whether the session expires if (! Isset ($ _ SESSION ["username"]) | time () <($ _ SESSION ['username'] + 1200) {echo "log on first
"; Echo 'Click here to log on!
'; Exit ;}

? Cancel session

Session_start ();/*** delete all session variables .. You can also delete them one by one using unset ($ _ SESSION [xxx. * ***/$ _ SESSION = array ();/*** delete sessin id. because the session is cookie-based by default, setcookie is used to delete the cookie containing the session id. * **/if (isset ($ _ COOKIE [session_name ()]) {setcookie (session_name (), '', time ()-42000 ,'/');} // Finally, the session is completely destroyed. session_destroy (); // jump to the login page Header ("HTTP/1.1 303 See Other"); Header ("Location: login.html ");

? Ps: session_start (); there cannot be any output before, and it must be added to the top of the page

Otherwise, the system reports the following :? Session_start () [function. session-start]: Cannot send session cache limiter-headers? Already sent?

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.