PHP login and logout example code_PHP tutorial

WBOY
Release: 2016-07-13 17:15:20
Original
1000 people have browsed it

Here our pm_user data table has not created a table. You can create it yourself. The following only introduces the program code for logging in using php and then logging out. Friends in need can refer to it.

login.htm

The code is as follows Copy code
 代码如下 复制代码





无标题文档



 


   
   
 


 


   
   
 


 


   
 





 代码如下 复制代码

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();
 }
}

Untitled Document

login.php
The code is as follows Copy code
function showPage() //Determine whether you are logged in or not. Jump directly to the login page if you are not logged in { 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)); //Determine user name $isPwd = $isUser ? $row["u_pwd"] == $_COOKIE["pwd"] : false; //Judge password 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("You are not logged in yet! Returning to the login page...");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>';
 }
}

复制代码
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.bkjia.com/PHPjc/628868.htmlwww.bkjia.comtrue
http://www.bkjia.com/PHPjc/628868.html
TechArticle
这里我们pm_user是数据表没有创建表,大家可以自己行创建了,下面只介绍利用php登录然后再退出登录的程序代码,有需要的朋友可进行参考...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!