Home > Backend Development > PHP Tutorial > PHP creates and uses session cookie variables_PHP tutorial

PHP creates and uses session cookie variables_PHP tutorial

WBOY
Release: 2016-07-21 14:57:27
Original
985 people have browsed it

// session start
session_start(); // Start a session. If you want to use the session program, you must add this sentence at the beginning.
$_SESSION[' user_id'] = '123';//Assign a value to a session variable. If the variable does not exist, create it

echo $_SESSION['user_id'];//Access the session variable

$ _SESSION = array();//Clear all session variables

session_destroy();//Clear session ID
// session end

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

echo $_COOKIE['user_id'];//Accessing the cookie variable is the same as the variable variable

setcookie('user_id' ,0,time()-1);//Delete cookie variable
// codie end

// This code is not executable. It just lists all the usage methods here. The actual functions should be different. The use of different pages will be demonstrated in the following examples
?>
// session start
session_start(); // Start a session. If you want to use the session program, it is best to Be sure to add this sentence before
$_SESSION['user_id'] = '123';//Assign a value to a session variable. If the variable does not exist, create it

echo $_SESSION['user_id'];//Access session variables

$_SESSION = array();//Clear all session variables

session_destroy();//Clear session ID
// session end

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

echo $_COOKIE['user_id'];//Access cookie variables are the same as variable variables

setcookie('user_id',0,time()-1);//Delete cookie variable
// codie end

// This code is not runnable, but all usage methods are listed here. In fact, different functions should be used on different pages, which will be demonstrated in the following examples
?>
Cookie, session is WEB Ways for applications to maintain user state

Cookie is the saved client information, which is sent to the server when the client connects to the server.

Session is information stored on the server. From this perspective, session is more secure than cookie.
When a session is created, the server returns an encrypted session id to the client to identify the user. The session id is usually stored in a cookie. Passed by URL when unavailable

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364050.htmlTechArticle?php // session start session_start(); // Start a session, if you want to use the session program, the beginning must be To add this $_SESSION['user_id'] = '123';//Assign a value to a session variable...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template