這裡我們pm_user是數據表沒有創建表,大家可以自己行創建了,下面只介紹利用php登錄然後再退出登錄的程序代碼,有需要的朋友可進行參考。
login.htm
代碼如下 復制代碼<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="user"></label>
<input type="text" name="user" id="user" />
</p>
<p>
<label for="pwd"></label>
<input type="text" name="pwd" id="pwd" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>
</body>
</html>
login.php
function showPage() //判斷是否登錄 未登錄直接跳轉至登錄頁面
{
if(!isset($_SESSION["user"]) && !isset($_SESSION["pwd"]))
{
if(isset($_COOKIE["user"]) && isset($_COOKIE["pwd"]))
{
$sql = "select * from pm_user where u_user='$_COOKIE[user]'";
$query = mysql_query($sql);
$isUser = is_array($row = mysql_fetch_array($query)); //判斷用戶名
$isPwd = $isUser ? $row["u_pwd"] == $_COOKIE["pwd"] : false; //判斷密碼
if($isPwd)
{
$_SESSION["user"] = $_COOKIE["user"];
$_SESSION["pwd"] = $_COOKIE["pwd"];
$_SESSION["id"] = $_COOKIE["id"];
$_SESSION["name"] = $_COOKIE["name"];
}
}
}
if(!isset($_SESSION["user"]) && !isset($_SESSION["pwd"]))
{
echo '<script>alert("你還沒登錄!正在返回登錄頁面...");location.href="index.php";</script>';
exit();
}
}
退出登錄
out.php
代碼如下 復制代碼function loginOut() //登出函數
{
if(isset($_GET["login"]) == 'out')
{
$_SESSION["user"] = '';
$_SESSION["pwd"] = '';
$_SESSION["id"] = '';
$_SESSION["name"] = '';
session_destroy();
echo '<script>alert("退出成功!");location.href="index.php"; </script>';
}
}