php+Mysql 頁面登錄代碼
阿新 • • 發佈:2017-10-26
信息 退出 mys 每次 reat ctype 檢測 require 之前
登錄界面設置:
<?php
/**
* Created by xx.
* User: msi
* Date: 2017/10/26
* Time: 18:12
*/
//session每次用之前都要啟用!!,用start!
session_start();
//var_dump($_SESSION);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="login_post.php" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="uname" placeholder="請輸入賬號"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="pwod" placeholder="請輸入密碼"></td>
</tr>
</table>
<input type="submit" value="登錄">
</form>
</body>
</html>
登錄處理界面設置代碼:
<?php
/**
* Created by xx.
* User: msi
* Date: 2017/10/26
* Time: 18:15
*/
session_start();
header("content-type:text/html; charset=utf-8");
require_once "../Public/DBDA.class.php";
$db = new DBDA();
//先查輸入的用戶名,再根據輸入的用戶名去數據庫查密碼,最後驗證密碼是否和Mysql裏面的密碼相匹配!
$sql = "select * from t_user WHERE uname = ‘{$_POST["uname"]}‘and pwod = ‘{$_POST["pwod"]}‘";
//只要不是1就行
$result = $db->strQuery($sql);
if ($result !="0"){
echo "登陸成功";
$_SESSION[‘user‘] = array(
‘uname‘=>‘user1‘,
);
// var_dump($_SESSION);
}else{
echo "登錄失敗";
}
//檢測session信息->
如果session信息為空的話,那麽就跳轉登錄界面
if (empty($_SESSION[‘user‘])){
header("location:login.php");
}
//退出登錄,銷毀賬號信息;
unset($_SESSION);
這樣就完成啦!
php+Mysql 頁面登錄代碼