PHP中cookie和session学习

cookie的测试:

<?php
/*
 * php cookie和session详解
 * cookie:保存在客户端,当浏览器禁用cookie时cookie既不能使用,
 * 但是cookie只要没有清除可以保存自己设定的时间,
 * cookie 不是实时生效的,是在第二次才会生效的,或者刷新或者跳转页面之后才会生效
 * 而session会随着浏览器的关闭而注销掉
 * session:保存在服务器端,当浏览器关闭时session即会注销掉
 * 但是session因为保存在服务器端,相对来说安全性更高
 */

/*
 * bool setcookie  ( string $name  [, string $value  [, int $expire  = 0  [, string $path  [, string $domain  [, bool $secure  = false  [, bool $httponly  = false  ]]]]]] )
 * name : cookie的名字
 * value :cookie的值
 * expire : cookie的到期时间,默认浏览器关闭就到期
 * path :规定 cookie的服务器路径,例如为 '/',则在整个作用域内都可以使用这个cookie,默认是当前作用域
 * domain : 规定 cookie 的域名
 * secure : 规定是否通过安全的 HTTPS 连接来传输 cookie
*/


//如果选择了设置cookie则进去设置cookie,没有则不进去设置
if( isset($_POST["mycookies"]) && $_POST["mycookies"]=="set" )
{
	//如果定义了这两个变量则进去设置cookie
	if( isset($_POST["username"]) && isset($_POST["password"]) )
	{
		//如果变量的值有一个为空则说明用户没有填写数据,不改变cookie的值
		if( $_POST["username"] || $_POST["password"] )
		{
			//setcookie() 函数必须位于 <html> 标签之前。
			setcookie("id", $_POST["username"], time()+$_POST["time"]);
			setcookie("pw", $_POST["password"], time()+$_POST["time"]);
			//下面做的目的就是使得cookie能够在不刷新的情况下立即生效
			//echo "<script type=\"text/javascript\">location.href=\"test5.php\"</script>";
		}
	}
}
//如果设置了cookie,则将其读出
if( isset($_COOKIE["id"]) && isset($_COOKIE["pw"]) )
{
	echo "username:".$_COOKIE["id"]."<br />password:".$_COOKIE["pw"];
}
//清除那两个cookie,这里使用REQUEST的原因是因为链接默认以get方法传送而不是post,也可以用GET全局数组得到值
if( isset($_REQUEST["motion"]) && $_REQUEST["motion"]=="clear" )
{
	setcookie("id", "");
	setcookie("pw", "");
	/*下面做的目的就是使得cookie能够在不刷新的情况下立即生效,方法就是自动让他做了一下跳转,而不是人工去做,所以没有
	感觉到,其实原理没有变,cookie还是要在第二次才能生效的,这里也需要注意了,如果没有下面的这句话,一定要记得现在页面
	跳转的位置是test5.php?motion=clear而不是test5.php,如果没有注意到页面一直在test5.php?motion=clear页面
	的时候就会发现cookie总是为空,总是没有cookie的出现
	*/
	echo "<script type=\"text/javascript\">location.href=\"test5.php\"</script>";
}

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>PHP学习</title>
<style type="text/css" >
body{text-align:center; margin-top:150px;}
</style>
<script type="text/javascript">
//location.href="test5.php";
</script>
</head>
<body>
<form name="test5" action="" method="post">
<a href="test5.php?motion=clear">清除cookie</a><br />
username : <input type="username" name="username"/><br />
password : <input type="password" name="password"/><br />
<input type="radio" checked="checked" name="mycookies" value="set" />设置cookie
<input type="radio" name="mycookies" value="unset" />不设置cookie
保存时间:
<select name="time">
	<option value="1">1秒钟</option>
	<option value="60">1分钟</option>
	<option value="3600">1小时</option>
	<option value="3600*24">1天</option>
	<option value="3600*24*30">1个月</option>
	<option value="3600*24*30*365">1年</option>
</select><br />
<input type="submit" value="提交"/>
</form>
</body>
</html>


session的测试:

<?php
//启动session,很多人都说session_start()必须在页面输出第一条语句之前完成,否则出错
//但是我测试了一下 echo "session之前输出<br />";并没有报错
//session不需要跳转之类的操作就会实时生效
 session_start();

//如果选择了设置session则进去设置session,没有则不进去设置
if( isset($_POST["mysessions"]) && $_POST["mysessions"]=="set" )
{
	//如果定义了这两个变量则进去设置session
	if( isset($_POST["username"]) && isset($_POST["password"]) )
	{
		//如果变量的值有一个为空则说明用户没有填写数据,不改变session的值
		if( $_POST["username"] || $_POST["password"] )
		{
			//在PHP5之后都是直接使用全局数组$_SESSION直接设置session,下面的步骤就是设置session了
			$_SESSION["id"]=$_POST["username"];
			$_SESSION["pw"]=$_POST["password"];
		}
	}
}
//如果设置了session,则将其读出
if( isset($_SESSION["id"]) && isset($_SESSION["pw"]) )
{
	echo "username:".$_SESSION["id"]."<br />password:".$_SESSION["pw"];
}
//清除那两个session,这里使用REQUEST的原因是因为链接默认以get方法传送而不是post,也可以用GET全局数组得到值
if( isset($_REQUEST["motion"]) && $_REQUEST["motion"]=="clear" )
{
	//这样就可以清除session了,和使用一般的变量一样方便,只不过是多了一个启动session的操作
	unset($_SESSION["id"]);
	unset($_SESSION["pw"]);
        //这是清除所有session的方法
        session_destroy();
}

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>PHP学习</title>
<style type="text/css" >
body{text-align:center; margin-top:150px;}
</style>
<script type="text/javascript">
//location.href="test5.php";
</script>
</head>
<body>
<form name="test5" action="" method="post">
<a href="test5.php?motion=clear">清除session</a><br />
username : <input type="username" name="username"/><br />
password : <input type="password" name="password"/><br />
<input type="radio" checked="checked" name="mysessions" value="set" />设置session
<input type="radio" name="mysessions" value="unset" />不设置session<br />
<input type="submit" value="提交"/>
</form>
</body>
</html>

截自PHP帮助文档回复:

<?php
// session start

//  It is VERY important to include a Period if using
// a whole domain.  (.yourdomain.com)
// It is VERY important to set the root path your session will always
// operate in... (/members) will ensure sessions will NOT be interfered
// with a session with a path of say (/admin) ... so you can log in
// as /admin and as /members... NEVER do unset($_SESSION)
// $_SESSION=array(); is preferred, session_unset();  session_destroy();

session_set_cookie_params(0, '/members', '.yourdomain.com', 0, 1);
session_start();
$_SESSION = array();
session_unset();
session_destroy();

session_set_cookie_params(0, '/members', '.yourdomain.com', 0, 1);
session_start();

$_SESSION['whatever'] = 'youwhat';

// session destroying 

// To be safe, clear out your $_SESSION array
// Next, what most people do NOT do is delete the session cookie!
// It is easy to delete a cookie by expiring it long before the current time.
// The ONLY WAY to delete a cookie, is to make sure ALL parameters match the
// cookie to be deleted...which is easy to get those params with 
// session_get_cookie_params()...
// FInally, use  session_unset(); and session_destroy(); in this order to ensure
// Chrome, IE, Firefox and others, are properly destroying the session.
$_SESSION = array();
if (ini_get('session.use_cookies'))
{
    $p = session_get_cookie_params();
    setcookie(session_name(), '', time() - 31536000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
 }
session_unset();
session_destroy();

// AJAX and SESSIONS.
// Example... you start a session based PHP page, which then calls an Ajax (XMLHTTP) authenticated
// using the SAME SESSION to Poll and output the data, for example.  But, you notice when you
// try to start the Polling AJAX call always HANGS and seems to hang at the session_start().
// This is because the session is opened in the first page, calls the AJAX polling example, and
// tries to open the same session (for authentication) and do the AJAX call, you MUST call
// session_write_close(); meaning you are done writing to the $_SESSION variable, which really
// represents a file that must be CLOSED with session_write_close();....
// THAN you can call your AJAX Polling code to reopen the same session and do its polling...
// Normally, the $_SESSION is closed automatically when the script is closed or finished executing
// So, if you need to keep a PHP page running after opening a SESSION, simply close it when finished
// writing to $_SESSION so the AJAX polling page can authenticate and use the same session in a
// seperate web page...

session_write_close();

?> 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值