PHP creates and uses session cookie variables

Source: Internet
Author: User
Tags empty session id variables sessions setcookie client

<?php

Session Start

Session_Start (); To start a session, if you want to use the sessions program, you must add this sentence to the front

$_session[' user_id '] = ' 123′;//assigns a value to a session variable that is created if the variable does not exist

echo $_session[' user_id '];//access session variable

$_session = Array ();//Empty all session variables

Session_destroy ()//purge session ID

Session End

Cookie Start

Setcookie (' user_id ', 123);//Create a cookie variable user_id=123

echo $_cookie[' user_id '];//access to COOKIE variables and workaround variables

Setcookie (' user_id ', 0,time ()-1);//Delete cookie variable

Codie End

The code is not running, just listing all the usage methods here, and actually using different features on different pages, will demonstrate in the following example

?>

<?php

Session Start

Session_Start (); To start a session, if you want to use the sessions program, you must add this sentence to the front

$_session[' user_id '] = ' 123′;//assigns a value to a session variable that is created if the variable does not exist

echo $_session[' user_id '];//access session variable

$_session = Array ();//Empty all session variables

Session_destroy ()//purge session ID

Session End

Cookie Start

Setcookie (' user_id ', 123);//Create a cookie variable user_id=123

echo $_cookie[' user_id '];//access to COOKIE variables and workaround variables

Setcookie (' user_id ', 0,time ()-1);//Delete cookie variable

Codie End

The code is not running, just listing all the usage methods here, and actually using different features on different pages, will demonstrate in the following example

?>

Cookie,session is a way for Web applications to maintain user state

Cookies are information stored by the client that is sent to the server when the client connects to the server.

Session is stored on the server side of the information, from this angle session relative to the cookie more secure

When the session is created, the server returns to the client a cryptographic session ID to identify the user, which is generally stored in a cookie that is passed by the URL when the cookie is not available

The above code shows how to create and use session cookie variables



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.