这里我们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>';
- }
- }
|