【PHP原生】Session登錄判斷與注銷


1、判斷是否登錄(check_login.php)

所有后台操作都要加上,用於權限控制
<?php header("Content-type: text/html; charset=utf-8"); session_start(); if($_SESSION['username']==""){ echo "<script>alert('請先登錄!');window.location.href='index.php';</script>"; exit(); } ?>

2、用戶登錄並保持會話

2.1、登錄表單(包含判斷登錄狀態功能)
<formname="form"method="post"action="checkuser.php">
<!--
<?php if(!isset($_SESSION['username'])){//判斷是否登錄
?>
--> 用戶名:<inputname=usersize="10"><br/> //沒登錄則顯示輸入用戶名密碼登錄
密碼:<inputname=pwdtype=password>
<inputtype='submit'value='登錄'name='submit' />
<inputtype='reset'value='重置'name='reset' />
 
<!--
<?php }else{ //如果已登錄則顯示提示語
echo '歡迎您的光臨!'; } ?>  
--> 
 
</form>
2.2、登錄表單處理(checkuser.php)
<?php session_start(); include "inc/conn.php"; $username=$_POST[user]; $password=$_POST[pwd]; $sql=mysql_query("select * from user where username='".$username."' and password='".$password."'"); if ($result = mysql_fetch_array($sql)){    $_SESSION[username]=$username; //其他頁面通過判斷是否設置$_SESSION[username]來判斷登錄狀態
?>
 
<scriptlanguage="javascript"> alert("登錄成功");window.location.href="file.php"; </script>
 
<?php }else{ ?>
 
<scriptlanguage="javascript"> alert("對不起,您輸入的用戶名或密碼不正確,請重新輸入!");window.location.href="index.php"; </script>
 
<?php } ?>
3、安全退出:
<?php session_start();//啟動會話
session_unset();//刪除會話
session_destroy();//結束會話
header("location: index.php"); ?>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM