PHP Session and Cookie

Source: Internet
Author: User
Tags php session set cookie setcookie

Knowledge Points:

Session is the server will be generated from the Web page of the information in an array into a PHP file, the resulting global variables, you can call this data in other pages under the system.

Cookies are similar to the session principle, but they are stored to the user's computer, not to the server.

Function:

Session_Start (); PHP script to open the session function, it is recommended to write in the first line of PHP.

SESSION assignment: $_session[' id '] = 1;

Session detects if the variable is registered: session_is_registered (' id '), returns TRUE or false; Can be equated to isset ($_session[' id ');

Session Destruction: Session_unset (), no parameters, no return value, equivalent to Session_destroy ();

SESSION single-Variable Destruction: unset ($_session[' id '));

Setcookie (Name,value,lifetime,path,domain): Name is the index name of the cookie, value is the cookie, the lifetime time to live, the path cookie holds the address, domain Store the domain name.

Code:

<?php
Session_Start ();
$_session[' name '] = "name";
echo $_session[' name '];//output name
if (session_is_registered (' name ') ===false) {

This variable was not found in echo ' Session! ';
}else{

This variable exists in echo ' Session! ‘;
}
Setcookie (' name ', $_session[' name '],time () +3600);//Set Cookie to live for one hour
echo $_cookie[' name '];//output name
unset ($_session[' name ');//Delete SESSION variable
Session_destroy ();
?>

Page Tip:

PHP Session and Cookie

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.