|
1、數(shù)據(jù)庫(kù)連接配置頁(yè)面:connectvars.php
復(fù)制代碼 代碼如下:
<?php
//數(shù)據(jù)庫(kù)的位置
define('DB_HOST', '127.0.0.1');
//用戶(hù)名
define('DB_USER', 'root');
//口令
define('DB_PASSWORD', '19900101');
//數(shù)據(jù)庫(kù)名
define('DB_NAME','test') ;
?>
2、登錄頁(yè)面:logIn.php
復(fù)制代碼 代碼如下:
<?php
//插入連接數(shù)據(jù)庫(kù)的相關(guān)信息
require_once 'connectvars.php';
//開(kāi)啟一個(gè)會(huì)話(huà)
session_start();
$error_msg = "";
//如果用戶(hù)未登錄,即未設(shè)置$_SESSION['user_id']時(shí),執(zhí)行以下代碼
if(!isset($_SESSION['user_id'])){
if(isset($_POST['submit'])){//用戶(hù)提交登錄表單時(shí)執(zhí)行如下代碼
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$user_username = mysqli_real_escape_string($dbc,trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc,trim($_POST['password']));
if(!empty($user_username)&&!empty($user_password)){
//MySql中的SHA()函數(shù)用于對(duì)字符串進(jìn)行單向加密
$query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND "."password = SHA('$user_password')";
$data = mysqli_query($dbc,$query);
//用用戶(hù)名和密碼進(jìn)行查詢(xún),若查到的記錄正好為一條,則設(shè)置SESSION和COOKIE,同時(shí)進(jìn)行頁(yè)面重定向
if(mysqli_num_rows($data)==1){
$row = mysqli_fetch_array($data);
$_SESSION['user_id']=$row['user_id'];
$_SESSION['username']=$row['username'];
setcookie('user_id',$row['user_id'],time()+(60*60*24*30));
setcookie('username',$row['username'],time()+(60*60*24*30));
$home_url = 'loged.php';
header('Location: '.$home_url);
}else{//若查到的記錄不對(duì),則設(shè)置錯(cuò)誤信息
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}else{
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}
}else{//如果用戶(hù)已經(jīng)登錄,則直接跳轉(zhuǎn)到已經(jīng)登錄頁(yè)面
$home_url = 'loged.php';
header('Location: '.$home_url);
}
?>
<html>
<head>
<title>Mismatch - Log In</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h3>Msimatch - Log In</h3>
<!--通過(guò)$_SESSION['user_id']進(jìn)行判斷,如果用戶(hù)未登錄,則顯示登錄表單,讓用戶(hù)輸入用戶(hù)名和密碼-->
<?php
if(!isset($_SESSION['user_id'])){
echo '<p class="error">'.$error_msg.'</p>';
?>
<!-- $_SERVER['php_SELF']代表用戶(hù)提交表單時(shí),調(diào)用自身php文件 -->
<form method = "post" action="<?php echo $_SERVER['php_SELF'];?>">
<fieldset>
<legend>Log In</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username"
value="<?php if(!empty($user_username)) echo $user_username; ?>" />
<br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password"/>
</fieldset>
<input type="submit" value="Log In" name="submit"/>
</form>
<?php
}
?>
</body>
</html>
效果圖:
3、登入頁(yè)面:loged.php
復(fù)制代碼 代碼如下:
<?php
//使用會(huì)話(huà)內(nèi)存儲(chǔ)的變量值之前必須先開(kāi)啟會(huì)話(huà)
session_start();
//如果會(huì)話(huà)沒(méi)有被設(shè)置,查看是否設(shè)置了cookie
if(!isset($_SESSION['user_id'])){
if(isset($_COOKIE['user_id'])&&isset($_COOKIE['username'])){
//用cookie給session賦值
$_SESSION['user_id']=$_COOKIE['user_id'];
$_SESSION['username']=$_COOKIE['username'];
}
}
//使用一個(gè)會(huì)話(huà)變量檢查登錄狀態(tài)
if(isset($_SESSION['username'])){
echo 'You are Logged as '.$_SESSION['username'].'<br/>';
echo '<a href="logOut.php"> Log Out('.$_SESSION['username'].')</a>';
}
/**在已登錄頁(yè)面中,可以利用用戶(hù)的session如$_SESSION['username']、
* $_SESSION['user_id']對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢(xún),可以做好多好多事情*/
?>
效果圖:
4、注銷(xiāo)session與cookie頁(yè)面:logOut.php(注銷(xiāo)后重定向到lonIn.php)
復(fù)制代碼 代碼如下:
<?php
/**同時(shí)注銷(xiāo)session和cookie的頁(yè)面*/
//即使是注銷(xiāo)時(shí),也必須首先開(kāi)始會(huì)話(huà)才能訪(fǎng)問(wèn)會(huì)話(huà)變量
session_start();
//使用一個(gè)會(huì)話(huà)變量檢查登錄狀態(tài)
if(isset($_SESSION['user_id'])){
//要清除會(huì)話(huà)變量,將$_SESSION超級(jí)全局變量設(shè)置為一個(gè)空數(shù)組
$_SESSION = array();
//如果存在一個(gè)會(huì)話(huà)cookie,通過(guò)將到期時(shí)間設(shè)置為之前1個(gè)小時(shí)從而將其刪除
if(isset($_COOKIE[session_name()])){
setcookie(session_name(),'',time()-3600);
}
//使用內(nèi)置session_destroy()函數(shù)調(diào)用撤銷(xiāo)會(huì)話(huà)
session_destroy();
}
//同時(shí)將各個(gè)cookie的到期時(shí)間設(shè)為過(guò)去的某個(gè)時(shí)間,使它們由系統(tǒng)刪除,時(shí)間以秒為單位
setcookie('user_id','',time()-3600);
setcookie('username','',time()-3600);
//location首部使瀏覽器重定向到另一個(gè)頁(yè)面
$home_url = 'logIn.php';
header('Location:'.$home_url);
?>
php技術(shù):php中如何同時(shí)使用session和cookie來(lái)保存用戶(hù)登錄信息,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。