php登錄與退出登錄實(shí)例代碼
來源:程序員人生 發(fā)布時(shí)間:2014-05-10 04:03:18 閱讀次數(shù):3663次
這里我們pm_user是數(shù)據(jù)表沒有創(chuàng)建表,大家可以自己行創(chuàng)建了,下面只介紹利用php登錄然后再退出登錄的程序代碼,有需要的朋友可進(jìn)行參考.
login.htm
實(shí)例代碼如下:
- <!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>無標(biāo)題文檔</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
實(shí)例代碼如下:
- 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("你還沒登錄!正在返回登錄頁(yè)面...");location.href="index.php";</script>';
- exit();
- }
- }
退出登錄
out.php
實(shí)例代碼如下:
- 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>';
- }
- }
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)