• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php基础教程——3cookie跟session

php 搞代码 3年前 (2022-01-24) 20次浏览 已收录 0个评论

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:

<?php if (isset($_POST['submitted'])){	setcookie('font-size', $_POST['font_size'], time() + 1000000000, '/', '', 0);	setcookie('font-color', $_POST['font_color'], time() + 1000000000, '/', '', 0);	$msg = '

setted!

';}?> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> test <body> <?phpif (isset($msg)){ print $msg;}?>

Choose your preferences:

Font Size x-small x-large Font Color Gray Green

This 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:

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> test  <body> <?phpdefine('TITLE', 'Login');if (isset($_POST['submitted'])) {	if ((!empty($_POST['name'])) && (!empty($_POST['password']))){		if ((strtolower($_POST['name']) == 'yf') && ($_POST['password'] == '123456')){// name and password are correct.			session_start();			$SESSION['name'] = $_POST['name'];			$SESSION['time'] = time();						print '

The session content:' .$SESSION['name']."\n".$SESSION['time'].'

'; unset($_SESSION); session_destroy(); print 'destroy()!'; } else { print '

name or password is worry!

'; } } else { print '

make sure you enter both name and password!

'; }}else {

2本文来源gao!daima.com搞$代!码网

搞代gaodaima码print '

Name:

Password:

'; }?>

This is the foot of the document

测试裁图:

结果:


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php基础教程——3cookie跟session

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址