php使用cookie實現簡單登入功能

2021-10-09 04:50:00 字數 2859 閱讀 3943

cookie 是用來將**上的資料記錄在客戶端的技術,這種web技術讓伺服器將一些資料存放於客戶端。比如當使用者成功登入**後,伺服器會把使用者資訊儲存到使用者的cookie中,當再次訪問同乙個**的其他指令碼時就會攜帶cookie中的資料一起訪問,在伺服器的每乙個指令碼中都可以接受攜帶的cooike資料,不需要每次訪問乙個頁面就重新輸入一次登入者資訊。

登入頁面**

<?php 

header

('content-type:text/html;charset=utf-8');

//設定編碼格式防止亂碼if(

isset

($_post

['submit'])

)else

}else

}?>

<

!doctype html>

"en"

>

"utf-8"

>

"viewport"

content=

"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"

>

"x-ua-compatible" content=

"ie=edge"

>

document<

/title>

<

/head>

登入成功頁面**

<?php 

header

('content-type:text/html;charset=utf-8');

//設定編碼格式防止亂碼if(

isset

($_cookie

['username'])

&&$_cookie

['username'

]===

'ahua'

)else

?>

增加乙個跳轉頁面和登出功能

跳轉頁面**

<?phpif (

!isset

($_get

['url'])

||!isset

($_get

['info'])

)?>

<

!doctype html>

"en"

>

"utf-8"

>

"viewport"

content=

"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"

>

'refresh' content=

"3;url=<?php echo $_get

['url'

]?>"

>

//延遲三秒,跳轉到傳入的url位址

document<

/title>

<

/head>

"text-align: center"

>

<?php

echo

$_get

['info'

]?>

<

/div>

//輸出提示內容

<

/body>

<

/html>增加後的登入頁面

<?phpif (

isset

($_post

['submit'])

)else

}else

}?>

<

!doctype html>

"en"

>

"utf-8"

>

"viewport"

content=

"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"

>

"x-ua-compatible" content=

"ie=edge"

>

document<

/title>

<

/head>

"post" action=

"login.php"

>

賬號:"text" name=

"username"

>

<

/br>

密碼:"password" name=

"password"

>

<

/br>

"submit" name=

"submit" value=

"登入"

>

<

/form>

<

/body>

<

/html>

登出頁面

<?phpif (

isset

($_cookie

['username'])

&&$_cookie

['username'

]===

'ahua'

)else

}

從左到右分別為:跳轉頁面名,登入成功頁面名,登入頁面名,登出頁面名

PHP使用cookie實現簡單的購物車功能

array 商品id1 array 名字 庫存等 商品id2 array 名字 庫存等 function.php 函式頁面 加入購物車function addcart gid,gname else else 刪除乙個商品 del是get請求的刪除id del get del function del...

php使用cookie實現購物車

檢視 原始碼時 看到的做下筆記 檔案包括 test.php,cart.php 一.test.php 商品顯示頁面 cart list empty cookie cart list unserialize cookie cart list array 例子使用陣列實際會用mysql product l...

PHP 使用cookie實現記住登入狀態

php使用cookie實現記住登入狀態,本文用最原始的方法講解如何實現記住登入狀態,給出3個步驟和具體實現 需要的朋友可以參考下。要實現記住密碼自動登入的功能,我們大多數據都是利用了客戶端的cookies來實現。php製作記住密碼自動登入的解決思路,其實也就是對session,cookies的操作 ...