php登录与退出登录实例代码
这里我们pm_user是数据表没有创建表,大家可以自己行创建了,下面只介绍利用php登录然后再退出登录的程序代码,有需要的朋友可进行参考.
login.htm
实例代码如下:
<!DOCTYPE html> <html> <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
实例代码如下:
<?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
实例代码如下:
<?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>';
}
}
?>本文地址:http://www.phprm.com/develop/fs2056.html
转载随意,但请附上文章地址:-)