標籤:
來源:http://blog.csdn.net/sysprogram/article/details/21107041
登入頁面
login.html 負責收集使用者填寫的登入資訊
[html] view plaincopy
- <html>
- <head>使用者登入</head>
- <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
- <p>
- <label for="username" class="label">使用者名稱:</label>
- <input id="username" name="username" type="text" class="input" />
- <p/>
- <p>
- <label for="password" class="label">密 碼:</label>
- <input id="password" name="password" type="password" class="input" />
- <p/>
- <p>
- <input type="submit" name="submit" value=" 確 定 " class="left" />
- </p>
- </form>
- </html>
登入處理
login.php 負責處理使用者登入與退出動作。
[php] view plaincopy
- <?php
- //登入
- if(!isset($_POST[‘submit‘])){
- exit(‘非法訪問!‘);
- }
- $username = htmlspecialchars($_POST[‘username‘]);
- $password = MD5($_POST[‘password‘]);
-
- //包含資料庫連接檔案
- include(‘conn.php‘);
- //檢測使用者名稱及密碼是否正確
- $check_query = mysql_query("select userid from user_list where username=‘$username‘ and password=‘$password‘ limit 1");
- if($result = mysql_fetch_array($check_query)){
- //登入成功
- session_start();
- $_SESSION[‘username‘] = $username;
- $_SESSION[‘userid‘] = $result[‘userid‘];
- echo $username,‘ 歡迎你!進入 <a href="my.php">使用者中心</a><br />‘;
- echo ‘點擊此處 <a href="login.php?action=logout">登出</a> 登入!<br />‘;
- exit;
- } else {
- exit(‘登入失敗!點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試‘);
- }
-
-
-
- //登出登入
- if($_GET[‘action‘] == "logout"){
- unset($_SESSION[‘userid‘]);
- unset($_SESSION[‘username‘]);
- echo ‘登出登入成功!點擊此處 <a href="login.html">登入</a>‘;
- exit;
- }
-
- ?>
使用者中心
my.php 是使用者中心,作為使用者登入檢測。
[php] view plaincopy
- <?php
- session_start();
-
- //檢測是否登入,若沒登入則轉向登入介面
- if(!isset($_SESSION[‘userid‘])){
- header("Location:login.html");
- exit();
- }
- //包含資料庫連接檔案
- include(‘conn.php‘);
- $userid = $_SESSION[‘userid‘];
- $username = $_SESSION[‘username‘];
- $user_query = mysql_query("select * from user_list where userid = ‘$userid‘ limit 1");
- $row = mysql_fetch_array($user_query);
- echo ‘使用者資訊:<br />‘;
- echo ‘使用者ID:‘,$userid,‘<br />‘;
- echo ‘使用者名稱:‘,$username,‘<br />‘;
- echo ‘<a href="login.php?action=logout">登出</a> 登入<br />‘;
- ?>
conn.php,用於串連資料庫
[php] view plaincopy
- <?php
-
- $conn = mysql_connect("127.0.0.1","root","") or die("資料庫連結錯誤".mysql_error());
- mysql_select_db("info_db",$conn) or die("資料庫訪問錯誤".mysql_error());
- mysql_query("set names gb2312");
- ?>
PHP驗證登入使用者名稱和密碼