PHPセッション制御PHPのCookie

このセクションでは、ユーザーが初めて Web サイトにアクセスするときにユーザー名とパスワードを再入力する必要がない例を通して Cookie について学びます。

まずはphpでCookieを設定する方法を紹介します。
php は Cookie を設定できる関数を提供します。この関数は次のとおりです:

bool setcookie  (
        string $名字
        [, string $值]
        [, int $过期时间  = 0]
        [, string $路径]
        [, string $域名]
        [, bool $安全  = false]
        [, bool $http只读  = false]
    );

パラメータの説明
$Name 必須。クッキーの名前を指定します。
$value オプション。 Cookieの値を指定します。
$有効期間はオプションです。 Cookieの有効期間を指定します。
$Path オプション。 Cookie のサーバー パスを指定します。
$ドメイン名はオプションです。 Cookie のドメイン名を指定します。
$セキュリティ オプション。 Cookie が安全な HTTPS 接続を介して送信されるかどうかを指定します。
$httpAndu オプション。 true の場合、js は Cookie を読み取ったり変更したりすることができないため、セキュリティが向上します。

一般的に、この関数では実際には上記ほど多くのパラメーターは使用しません: setcookie(cookie name, cookie value, cookie validity period);

そうです、たった 3 つです。このようにして、サーバー側で $_COOKIE['name'] を通じて Cookie を読み取ることができます。

ここに例を示します:
ファイルに cookie.php という名前を付けます。

インターネットで見られる最も一般的な例、つまりユーザー名とパスワードを入力して正常にログインする例をシミュレートしてみましょう。

テーブル user と 2 つのフィールド (ユーザー名とパスワード) を持つデータベース ログインを構築しましょう。

<?php
//第一次登陆的时候,通过用户输入的信息来确认用户
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
    $userName = $_POST['username'];
    $password = $_POST['password'];
    //从db获取用户信息
    //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
    $conn = mysqli_connect('localhost','root','root');

    mysqli_select_db($conn,'test');

    $sql = "select * from user where `username` = '$userName' ";
    $res = mysqli_query($conn,$sql);
    $row = mysqli_fetch_assoc($res);
    if ($row['password'] == $password) {
        //密码验证通过,设置cookies,把用户名和密码保存在客户端
        setcookie('username',$userName,time()+60*60*24*30);//设置时效一个月,一个月后这个cookie失效
        setcookie('password',$password,time()+60*60*24*30);
        //最后跳转到登录后的欢迎页面
        header('Location: welcome.php' . "?username=$userName");
    }
}

//再次访问的时候通过cookie来识别用户
if ( ($_COOKIE['username'] != null)  && ($_COOKIE['password'] != null) ) {
    $userName = $_COOKIE['username'];
    $password = $_COOKIE['password'];

    //从db获取用户信息
    //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
    $conn = mysqli_connect('localhost','root','root','test');
    $res = mysqli_query($conn,"select * from user where `username` =  '$userName' ");
    $row = mysqli_fetch_assoc($res);
    if ($row['password'] == $password) {
        //验证通过后跳转到登录后的欢迎页面
        header('Location: welcome.php' . "?username=$userName");
    }
}

?>
<html>
<head>

</head>
<body>
<form action="" method="POST">
    <div>
        用户名:<input type="text" name="username" />
        密  码:<input type="text" name="password" />
        <input type="submit" value="登录">
    </div>
</form>
</body>
</html>

<?php
$user = $_GET['username'];
?>
<html>
<head>

</head>
<body>
   welcome,<?php echo $user;?>
</body>
</html>

にジャンプするwelcome.phpのコード このように、初めてcookie.phpにアクセスした際にユーザー名とパスワードを入力する必要があり、入力後welcome.phpにジャンプします。次に、ブラウザを閉じて cookie.php を再度開きました。今回はユーザー情報の入力は求められませんでしたが、以前に保存した Cookie 情報がブラウザによって自動的にサーバーに送信されたため、直接 welcome.php にジャンプしました。サーバーは処理後、welcome.php に直接ジャンプします。サーバーは私たちを認識しています。私が以前にログインしたユーザーであることを認識しているため、Cookie テクノロジーを使用してステートレス HTTP プロトコルの状態を維持します。
これに従っていけばCookieが使えるようになると思います。

それ以外は何もありません! ! !のみ! ! !のみ! ! !重要なことを 3 回言います。安全ではないため、Cookie にはユーザー名とパスワードを含めません。重要な情報は Cookie に含めないでください。私たちの例は、Cookie を学習する一例にすぎません。

学び続ける
||
<?php //第一次登陆的时候,通过用户输入的信息来确认用户 if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) { $userName = $_POST['username']; $password = $_POST['password']; //从db获取用户信息 //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码 $conn = mysqli_connect('localhost','root','root'); mysqli_select_db($conn,'test'); $sql = "select * from user where `username` = '$userName' "; $res = mysqli_query($conn,$sql); $row = mysqli_fetch_assoc($res); if ($row['password'] == $password) { //密码验证通过,设置cookies,把用户名和密码保存在客户端 setcookie('username',$userName,time()+60*60*24*30);//设置时效一个月,一个月后这个cookie失效 setcookie('password',$password,time()+60*60*24*30); //最后跳转到登录后的欢迎页面 header('Location: welcome.php' . "?username=$userName"); } } //再次访问的时候通过cookie来识别用户 if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) { $userName = $_COOKIE['username']; $password = $_COOKIE['password']; //从db获取用户信息 //PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码 $conn = mysqli_connect('localhost','root','root','test'); $res = mysqli_query($conn,"select * from user where `username` = '$userName' "); $row = mysqli_fetch_assoc($res); if ($row['password'] == $password) { //验证通过后跳转到登录后的欢迎页面 header('Location: welcome.php' . "?username=$userName"); } } ?> <html> <head> </head> <body> <form action="" method="POST"> <div> 用户名:<input type="text" name="username" /> 密 码:<input type="text" name="password" /> <input type="submit" value="登录"> </div> </form> </body> </html>
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!