PHP会话控制之Cookie使用例子

一、什么是会话控制

1、http是一个无状态的协议,此协议无法来维护两个事务之间的联系。当一个用户在请求一个页面后在请求另外一个页面时,http将无法告诉我们这两个请求是来自同一个用户。会话控制思想就是能够在网站中跟踪一个用户,我们可以跟踪用户,就可以做到对用户的支持,并根据授权和用户身份显示不同内容,不同页面。

2、PHP会话控制是一种面向连接的可靠通信方式,通常根据会话控制记录判断用户的登录行为。PHP会话控制机制有两种:一种是基于Cookie的,另一种是基于Session的。

二、基于Cookie的使用说明

1、cookies是一种由服务器发送给客户端的片段信息,存储在客户端浏览器的内存或者在用户电脑的硬盘上,在客户对该服务的请求中发回它。

2、设置Cookie setcookie

函数: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的有效时间,选填。默认关闭浏览器后cookie失效

path: cookie在服务器上的有效路径

domain: 设置cookie在哪个域名下有效

secure: 指明cookie是否仅通过安全的HTTPS连接传送

例子:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);

echo $_COOKIE["TestCookie"];
print_r($_COOKIE);

setCookie("TestCookie", '', time()-3600);  //删除cookie
?>

3、Cookie一般默认存在:C:UsersAdministratorCookies 下,是一个隐藏文件夹

三、一个使用cookie来保存用户信息的程序例子

1、数据库连接文件:conn.inc.php:

<?php
        $mysqli=new mysqli("localhost", "root", "123456", "xsphpdb");
?>

2、公共引用文件:comm.php

<?php
        if(!$_COOKIE["isLogin"]){
                header("Location:login.php");
        }
?>

3、登录页面文件:login.php

<?php
        if(isset($_POST["sub"])){
                include "conn.inc.php";

                $sql="select id from users where name='{$_POST["name"]}' and password='".md5($_POST["password"])."'";

                $result=$mysqli->query($sql);

                if($result->num_rows > 0){
                        $row=$result->fetch_assoc();
                        $time=time()+60*60*24*7;
                        setCookie("username", $_POST["name"], $time);
                        setCookie("uid", $row["id"], $time);
                        setCookie("isLogin", 1, $time);

                        header("Location:index.php");
                }

                echo "用户名密码有误!<br>";
        }
?>
<html>
        <head>
                <title>用户登录</title>
        </head>
        <body>
                <form action="login.php" method="post">
                <table align="center"  width="300">
                        <caption><h1>用户登录</h1></caption>
                        <tr>
                                <th>用户名</th>
                                <td><input type="text" name="name"></td>
                        </tr>
                        <tr>
                                <th>密 码</th>
                                <td><input type="password" name="password"></td>
                        </tr>
                        <tr>

                                <td colspan="2" align="center">
                                        <input type="submit" name="sub" value="登 录">
                                </td>
                        </tr>
                </table>
                </form>
        </body>
</html>

4、登陆后跳转的首页文件:index.php

<?php
        include "comm.php";
        include "conn.inc.php";

        echo "用户<b>".$_COOKIE["username"]."</b>您好, 这是网站首页!";

        echo "你的权限如下:<br>";

        $sql="select allow_1, allow_2, allow_3, allow_4 from users where uid"]}'";

        $result=$mysqli->Query($sql);

        $user=$result->fetch_assoc();

        if($user["allow_1"]){
                echo "111111111111111111111111<br>";
        }
        if($user["allow_2"]){
                echo "2222222222222222<br>";
        }
        if($user["allow_3"]){
                echo "333333333333333333333<br>";
        }
        if($user["allow_4"]){
                echo "444444444444444444444444<br>";
        }

?>
        <a href="two.php">第二页</a> <br>
        <a href="three.php">第三页</a> <br>
        <a href="logout.php">退出</a> <br>

5、测试cookie取得信息的第二个页面:two.php

<?php
        include "comm.php";
        include "conn.inc.php";

        echo "用户<b>".$_COOKIE["username"]."</b>您好, 这是网站这二个页面!";

        echo "你的权限如下:<br>";

        $sql="select allow_1, allow_2, allow_3, allow_4 from users where uid"]}'";

        $result=$mysqli->Query($sql);

        $user=$result->fetch_assoc();

        if($user["allow_1"]){
                echo "111111111111111111111111<br>";
        }
        if($user["allow_2"]){
                echo "2222222222222222<br>";
        }
        if($user["allow_3"]){
                echo "333333333333333333333<br>";
        }
        if($user["allow_4"]){
                echo "444444444444444444444444<br>";
        }

?>
        <a href="two.php">第二页</a> <br>
        <a href="three.php">第三页</a> <br>
        <a href="logout.php">退出</a> <br>

6、测试cookie取得信息的第三个页面:three.php

<?php
        include "comm.php";
        include "conn.inc.php";

        echo "用户<b>".$_COOKIE["username"]."</b>您好, 这是网站第三个页面!";

        echo "你的权限如下:<br>";

        $sql="select allow_1, allow_2, allow_3, allow_4 from users where uid"]}'";

        $result=$mysqli->Query($sql);

        $user=$result->fetch_assoc();

        if($user["allow_1"]){
                echo "111111111111111111111111<br>";
        }
        if($user["allow_2"]){
                echo "2222222222222222<br>";
        }
        if($user["allow_3"]){
                echo "333333333333333333333<br>";
        }
        if($user["allow_4"]){
                echo "444444444444444444444444<br>";
        }

?>
        <a href="two.php">第二页</a> <br>
        <a href="three.php">第三页</a> <br>
        <a href="logout.php">退出</a> <br>