注意事项,session 储存在服务器端, cookie储存在客户端,cookie须要刷新一次页面才能生效php
Cookie 语法html
setcookie(string name,string value,int expire,string path,string domain,int secure)服务器
string name cookie名称cookie
string value cookie的值session
int expire 到什么时候结束dom
string path cookie的做用路径ide
string domain cookie的做用域函数
int secure 协议post
- setcookie("cookie","cookievalue",time()+3600,"/forum",".域名.com",1)//表示用HTTPS的协议,在域名.com的forum文件夹下,给cookie值为 cookievalue 3600秒
实例,制做一个简单的登陆判断ui
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <?
- /*判断是否从退出登陆连接来,若是是,执行清除COOKIE*/
- if($_GET[type] == "loginout"){
- setcookie("user","");
- setcookie("pass","");
- echo "<script>location.href='cookie.php'</script>";//由于cookie必须是刷新一次才生效因此自动刷新
- }
- /*判断是否输入了用户名和密码*/
- if($_POST[user] && $_POST[password])
- {
- setcookie("user",$_POST[user],time()+3600);//保存时间为1小时
- setcookie("pass",$_POST[password],time()+3600);
- echo "<script>location.href='cookie.php'</script>";//由于cookie必须是刷新一次才生效因此自动刷新
- }
- if ($_COOKIE[user] && $_COOKIE[pass])
- {
- /*输出用户名和密码*/
- echo "用户名:".$_COOKIE[user]."<br>";
- echo "密码:".$_COOKIE[pass]."<br>";
- echo "<a href='cookie.php?type=loginout'>退出登陆</a>";
- }
- ?>
- <form id="login" name="login" method="post" action="">
- <label for="user"></label>
- <table width="600" border="1">
- <tr>
- <td width="118" align="right">用户名:</td>
- <td width="466"><label for="user2"></label>
- <input type="text" name="user" id="user2" /></td>
- </tr>
- <tr>
- <td align="right">密码:</td>
- <td><label for="password"></label>
- <input type="text" name="password" id="password" /></td>
- </tr>
- <tr>
- <td align="right"> </td>
- <td><input type="submit" name="sub" id="button" value="登陆" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
session 语法
必须在输出内容前开启session 使用session_start()函数
$_SESSION[name]=name; 设置session
echo $_SESSION[name]; 取得session并打印
isset($_SESSION[name] ) 判断session
unset( $_SESSION[name]) 删除名为name的session
session_destory()删除全部的session
实例,制做一个简单的session登陆
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <?
- session_start();//必须在打印内容以前,也就是程序的最前方使用
- /*判断是否从退出登陆连接来,若是是,执行清除session*/
- if($_GET[type] == "loginout"){
- unset($_SESSION[user]);
- unset($_SESSION[pass]);
- }
- /*判断是否输入了用户名和密码*/
- if($_POST[user] && $_POST[password])
- {
- $_SESSION[user]=$_POST[user];
- $_SESSION[pass]=$_POST[password];
- }
- if ($_SESSION[user] && $_SESSION[pass])
- {
- /*输出用户名和密码*/
- echo "用户名:".$_SESSION[user]."<br>";
- echo "密码:".$_SESSION[pass]."<br>";
- echo "<a href='cookie.php?type=loginout'>退出登陆</a>";
- }
- ?>
- <form id="login" name="login" method="post" action="">
- <label for="user"></label>
- <table width="600" border="1">
- <tr>
- <td width="118" align="right">用户名:</td>
- <td width="466"><label for="user2"></label>
- <input type="text" name="user" id="user2" /></td>
- </tr>
- <tr>
- <td align="right">密码:</td>
- <td><label for="password"></label>
- <input type="text" name="password" id="password" /></td>
- </tr>
- <tr>
- <td align="right"> </td>
- <td><input type="submit" name="sub" id="button" value="登陆" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>