标签:php基础教程 cookie session cookie和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 = '<p>setted!</p>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> </head> <body> <?php if (isset($msg)){ print $msg; } ?> <p>Choose your preferences:</P> <form action='ws.php' method='POST'> <select name="font_size"> <option value=''>Font Size</option> <option value='x-small'>x-small</option> <option value='x-large'>x-large</option> </select> <select name="font_color"> <option value=''>Font Color</option> <option value='999'>Gray</option> <option value='0c0'>Green</option> </select> <input type="submit" name="submit" value="Set My Preferences"/> <input type="hidden" name="submitted" value="true"/> </form> <pre name="code" class="html"> <div><p>This is the foot of the document</p></div> </body> </html>
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:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> </head> <body> <?php define('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 '<h1>The session content:' .$SESSION['name']."\n".$SESSION['time'].'</h1>'; unset($_SESSION); session_destroy(); print 'destroy()!'; } else { print '<p> name or password is worry!</p>'; } } else { print '<p> make sure you enter both name and password!</p>'; } } else { print '<form action="ws.php" method="post"> <p> Name:<input type="text" name="name" size="20"/></p> <p>Password:<input type="password" name="password" "size="20" /></p> <input type="submit" value="send"> <input type="hidden" name="submitted" value="true"/> </form>'; } ?> <div><p>This is the foot of the document</p></div> </body> </html>
标签:php基础教程 cookie session cookie和session的使用
原文地址:http://blog.csdn.net/u014761420/article/details/42804109