search
Homephp教程php手册PHP创建和使用session cookie变量

// session start

session_start(); // 开始一个会话,如果要使用session程序最前面一定要加上这句

$_SESSION['user_id'] = ‘123′;//给一个session 变量赋值,如果该变量不存在即创建

echo $_SESSION['user_id'];//访问 session变量

$_SESSION = array();//清空所有session变量

session_destroy();//清除会话ID

// session end

// cookie start

setcookie(’user_id’,123);//创建一个cookie变量user_id=123

echo $_COOKIE['user_id'];//访问 cookie变量 和变通变量一样

setcookie(’user_id’,0,time()-1);//删除cookie变量

// codie end

// 该代码不可运行,只是将所有使用方法在这里列出,实际应该不同功能在不同页面使用,将在下面的例子中演示

?>

// session start

session_start(); // 开始一个会话,如果要使用session程序最前面一定要加上这句

$_SESSION['user_id'] = ‘123′;//给一个session 变量赋值,如果该变量不存在即创建

echo $_SESSION['user_id'];//访问 session变量

$_SESSION = array();//清空所有session变量

session_destroy();//清除会话ID

// session end

// cookie start

setcookie(’user_id’,123);//创建一个cookie变量user_id=123

echo $_COOKIE['user_id'];//访问 cookie变量 和变通变量一样

setcookie(’user_id’,0,time()-1);//删除cookie变量

// codie end

// 该代码不可运行,只是将所有使用方法在这里列出,实际应该不同功能在不同页面使用,将在下面的例子中演示

?>

cookie,session 是WEB应用程序保持用户状态的方法

cookie是保存的客户端的信息,由客户端连接服务器时发送到服务器的。

session是保存在服务端的信息,从这个角度session相对cookie更安全

当会话创建时服务器返回给客户端一个加密后的session id以标识用户身份,session id 一般保存在cookie当cookie不可用时由URL传递

上面的代码演示如何创建和使用session cookie变量



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment