php基础教程——3cookie和session
一、cookie
1.创建cookie,注:必须在发送其他任何信心之前从服务器发送到客户端,否则导致错误。
使用函数发送cookie: setcookie(name, value);
2.读取cookie
eg:setcookie('user', 'trout');
$cookie['user'];
3.添加参数
set(name, value, expiration, path, domain, sesure, httponly);
参数简介:
name键, value值,
expiration存在时间,
path和 domain限制在特定文件夹或域中才存在,
sesure值1表必须使用安全连接,反之值0表不必要,
httponly限制对cookie的访问,比如禁止javascript对cookie的访问。
4.删除cookie
使用通首次设置cookie时相同的参数,不设置值。
eg:setcookie('user', 'larry');
删除:setcookie('user', '');
编码测试:ws.php:
testchoose your preferences:
font sizex-smallx-largefont colorgraygreenthis is the foot of the document
二、session
1.session与cookie区别:
1>session将信息保存于服务器,cookie保存于客户端
2>session保存信息量更大
3>session更安全
2.创建session,注:必须在向web发信息之前调用
1>调用函数:session_start();
2>通过数组$_session进行数值记录:$_session[' email '];
3.访问session:
$_session[' email '];
4.删除session:session数据存在两个地方,故从两个地方删除:
1>session_start()
2>unset($_session);
3>session_destory(); //删除服务器上的
编码测试:ws.php:
testthis is the foot of the document
测试裁图:
结果:
