PHP ?? ?? ??
? ????? ???? ?? ????? ??? ? ??? ??? ????? ?? ??? ??? ?? ?? ?? ??? ?? ?????.
?? PHP?? ??? ???? ??? ???????.
php? ??? ??? ? ?? ??? ?????.
bool setcookie ( string $名字 [, string $值] [, int $過(guò)期時(shí)間 = 0] [, string $路徑] [, string $域名] [, bool $安全 = false] [, bool $http只讀 = false] );
???? ??
$Name ?????. ??? ??? ?????.
$value ????. ??? ?? ?????.
$????? ???????. ??? ?? ??? ?????.
$Path ????. ??? ?? ??? ?????.
$??? ?? ????. ??? ??? ??? ?????.
$Security ????. ??? ?? HTTPS ??? ?? ????? ??? ?????.
$httpannRead? ???????. true? ?? js? ??? ?? ??? ? ???? ??? ?????.
????? ? ??? ?? ???? ?? ?? ?? ????? ???? ????. setcookie(?? ??, ?? ?, ?? ?? ??)
?, ? 3??. ?? ???? ?? ??? $_COOKIE['name']? ?? ??? ?? ? ????.
?? ??? ????.
?? ??? cookie.php? ?????.
????? ? ? ?? ?? ???? ?? ??? ??? ????? ???? ????? ????? ??? ?????? ?????.
??? ???? ??? ?? ? ?????? ? ?? ??? ?? ?????? ???? ??? ?????.
<?php //第一次登陸的時(shí)候,通過(guò)用戶輸入的信息來(lái)確認(rèn)用戶 if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) { $userName = $_POST['username']; $password = $_POST['password']; //從db獲取用戶信息 //PS:數(shù)據(jù)庫(kù)連接信息改成自己的 分別為主機(jī) 數(shù)據(jù)庫(kù)用戶名 密碼 $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) { //密碼驗(yàn)證通過(guò),設(shè)置cookies,把用戶名和密碼保存在客戶端 setcookie('username',$userName,time()+60*60*24*30);//設(shè)置時(shí)效一個(gè)月,一個(gè)月后這個(gè)cookie失效 setcookie('password',$password,time()+60*60*24*30); //最后跳轉(zhuǎn)到登錄后的歡迎頁(yè)面 header('Location: welcome.php' . "?username=$userName"); } } //再次訪問(wèn)的時(shí)候通過(guò)cookie來(lái)識(shí)別用戶 if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) { $userName = $_COOKIE['username']; $password = $_COOKIE['password']; //從db獲取用戶信息 //PS:數(shù)據(jù)庫(kù)連接信息改成自己的 分別為主機(jī) 數(shù)據(jù)庫(kù)用戶名 密碼 $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) { //驗(yàn)證通過(guò)后跳轉(zhuǎn)到登錄后的歡迎頁(yè)面 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? ?? ??? ? ??? ??? ????? ???? ??, ?? ? .php? ?????. ?? ?? ????? ?? ?? cookie.php? ?????. ???? ??? ??? ????? ???? ???? ?? ?? Welcome.php? ??????. ??? ??? ?? ??? ????? ?? ???? ??? ????? ?????. ??? ??? ?, ?? Welcome.php? ?????. ??? ??? ?? ????! ?? ??? ???? ????? ?? ?? ??? ???? ?? ??? HTTP ????? ??? ?????.
?? ??? ??? ??? ? ??? ????.
? ??? ????! ! ! ??! ! ! ??! ! ! ??? ?? ? ? ????????. ????? ???? ??? ??? ????? ?? ????. ???? ??? ???? ?? ??? ??? ?? ??? ? ?? ?????. ??? ?? ?? ??? ? ?? ????.