亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

php session control Cookies in PHP

In this section, we learn about Cookie through an example where a user does not need to re-enter the user name and password when he visits the website for the first time.

First introduce how to set cookies in php.
php provides a function that allows us to set cookies. This function is:

bool setcookie  (
        string $名字
        [, string $值]
        [, int $過期時間  = 0]
        [, string $路徑]
        [, string $域名]
        [, bool $安全  = false]
        [, bool $http只讀  = false]
    );

Parameter Description
$Name Required. Specifies the name of the cookie.
$value Optional. Specifies the value of the cookie.
$Validity period Optional. Specifies the validity period of the cookie.
$Path Optional. Specifies the server path for cookies.
$Domain name Optional. Specifies the domain name for the cookie.
$Security Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.
$httpAndu Optional. If true, then js cannot read and change the cookie, which increases security.

Generally speaking, we don’t actually use as many parameters as above. For this function, we usually use it like this: setcookie(cookie name, cookie value, cookie validity period);

Yes, Just 3. In this way, we can read the cookie through $_COOKIE['name'] on the server side.

The following is an example:
We name the file: cookie.php.

Let’s simulate the most common example we see on the Internet: enter the user name and password and successfully log in.

Let's build a database login, which has a table user and two fields: username and password.

<?php
//第一次登陸的時候,通過用戶輸入的信息來確認用戶
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
    $userName = $_POST['username'];
    $password = $_POST['password'];
    //從db獲取用戶信息
    //PS:數(shù)據(jù)庫連接信息改成自己的 分別為主機 數(shù)據(jù)庫用戶名 密碼
    $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) {
        //密碼驗證通過,設(shè)置cookies,把用戶名和密碼保存在客戶端
        setcookie('username',$userName,time()+60*60*24*30);//設(shè)置時效一個月,一個月后這個cookie失效
        setcookie('password',$password,time()+60*60*24*30);
        //最后跳轉(zhuǎn)到登錄后的歡迎頁面
        header('Location: welcome.php' . "?username=$userName");
    }
}

//再次訪問的時候通過cookie來識別用戶
if ( ($_COOKIE['username'] != null)  && ($_COOKIE['password'] != null) ) {
    $userName = $_COOKIE['username'];
    $password = $_COOKIE['password'];

    //從db獲取用戶信息
    //PS:數(shù)據(jù)庫連接信息改成自己的 分別為主機 數(shù)據(jù)庫用戶名 密碼
    $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) {
        //驗證通過后跳轉(zhuǎn)到登錄后的歡迎頁面
        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>

The welcome.php code that jumps to

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

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

In this way, when I visit cookie.php for the first time, I need to enter the user name and password. After entering, I jump to welcome. .php. Then I closed the browser and opened cookie.php again. This time I was not asked to enter user information, but jumped directly to welcome.php, because the cookie information we saved before was automatically sent to the server by the browser, and the server did After processing, we jump directly to welcome.php. The server knows us! Knowing that I am the user who logged in before, we use cookie technology to maintain the state of the stateless HTTP protocol.
Do this again, I believe you can use cookies.

only! ! ! only! ! ! only! ! ! I have to say important things three times. We generally do not put usernames and passwords in cookies because it is not safe and can easily leak your own information. Please do not put important information in cookies. Ours is just an example of learning cookies.

Continuing Learning
||
<?php //第一次登陸的時候,通過用戶輸入的信息來確認用戶 if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) { $userName = $_POST['username']; $password = $_POST['password']; //從db獲取用戶信息 //PS:數(shù)據(jù)庫連接信息改成自己的 分別為主機 數(shù)據(jù)庫用戶名 密碼 $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) { //密碼驗證通過,設(shè)置cookies,把用戶名和密碼保存在客戶端 setcookie('username',$userName,time()+60*60*24*30);//設(shè)置時效一個月,一個月后這個cookie失效 setcookie('password',$password,time()+60*60*24*30); //最后跳轉(zhuǎn)到登錄后的歡迎頁面 header('Location: welcome.php' . "?username=$userName"); } } //再次訪問的時候通過cookie來識別用戶 if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) { $userName = $_COOKIE['username']; $password = $_COOKIE['password']; //從db獲取用戶信息 //PS:數(shù)據(jù)庫連接信息改成自己的 分別為主機 數(shù)據(jù)庫用戶名 密碼 $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) { //驗證通過后跳轉(zhuǎn)到登錄后的歡迎頁面 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>
submitReset Code