[英]Password Protection Using PHP and MySQL
我正在創建一個包含登錄和注冊功能的Web應用程序。 有兩個主要用戶,客戶端和1個管理員。 到目前為止,我已經能夠成功為客戶端創建一個注冊頁面,該頁面鏈接到mySQL數據庫。
以及客戶端和管理員的登錄頁面。 登錄后,客戶端或管理員將被重定向到其各自的儀表板。
我現在面臨的問題是-如果任何人訪問培訓人員信息中心網址中的網站類型,他們將被授予完全訪問權限和管理特權。 我希望出現一條消息,說“請登錄”
這是我目前在“ login.php”文件中使用的代碼的摘要:
<?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
<center><form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<div class="form-group">
<input type="text" name="username" id="username" class="form-control input-lg" placeholder="Username" tabindex="3">
</div>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="5">
</div>
<br /> <br /><input type="submit" name="submit" class="btn btn-success btn-block btn-lg" value="Login" /> </center>
</form>
<?php
} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$tusername = $_POST['username'];
$tpassword = $_POST['password'];
$sql = "SELECT * from client WHERE Client_username LIKE '{$username}' AND Client_password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
header('location:client_dash.html?msg=success');
}
$sql = "SELECT * from trainer WHERE trainer_username LIKE '{$tusername}' AND trainer_password LIKE '{$tpassword}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "<p>Invalid username/password combination</p>";
} else {
header('location:trainer_dash.php?msg=success');
}
}
?>
您可以使用SESSION變量來完成此操作
您的代碼將更改為。
<?php
ob_start();
session_start();
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$tusername = $_POST['username'];
$tpassword = $_POST['password'];
$sql = "SELECT * from client WHERE Client_username LIKE {$username}' AND Client_password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1)
{
echo "<p>Invalid username/password combination</p>";
}
else
{
$_SESSION['username']=$username;
header('location:client_dash.html?msg=success');
}
$sql = "SELECT * from trainer WHERE trainer_username LIKE '{$tusername}' AND trainer_password LIKE '{$tpassword}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1)
{
echo "<p>Invalid username/password combination</p>";
}
else
{
$_SESSION['tusername']=$tusername;
header('location:trainer_dash.php?msg=success');
}
}
?>
如果是client
則可以在儀表板上執行此操作
<?php
ob_start();
session_start();
if(!isset($_SESSION['username']))
{
die('Please log in first');
}
unset($_SESSION['username']);
*/rest code*/
?>
和類似的trainer
的儀表板
<?php
ob_start();
session_start();
if(!isset($_SESSION['tusername']))
{
die('Please log in first');
}
unset($_SESSION['tusername']);
*/rest code*/
?>
您正在取消設置會話變量,因為如果您不這樣做,那么它將僅在第一次使用,因為在那之后您的會話將被永久設置。因此,您必須unset
它們
您可以使用會議
重定向之前,請設置一個會話變量(不要忘記首先使用session_start()
啟動會話)
//to make sure the session_id() is different everytime the user logs in
session_regenerate_id();
//store the session_id in a variable
$_SESSION['trainer']=session_id();
然后在您的trainer_dash.php
,開始於:
session_start();
if(!isset($_SESSION['trainer'])||$_SESSION['trainer']!=session_id()){
echo 'You shouldn't be here';
}
到目前為止,答案很有效,因為它們阻止了一個特定的URL(或文件夾)。
如果有人在儀表板之一中輸入圖片的鏈接,則圖片將由Web服務器傳遞,因為沒有PHP腳本會禁止這樣做。
如果您需要可靠的解決方案,請嘗試將登錄/身份驗證作為應用程序前面的組件分開。 如果可能,請使用Web服務器過濾器。
在您的情況下,一個簡單的解決方案可能是使用PHP調用基本身份驗證並用密碼保護儀表板。 此處說明:
http://php.net/manual/de/features.http-auth.php
如果Web服務器看不到有效的身份驗證,則它將在解析任何PHP之前阻止對每種文件的各種訪問。
如果您需要更復雜的解決方案,將無法對Apache詳細信息進行一些研究(可能使用重寫規則或錯誤頁面),也無法嘗試使用與您的應用程序分離的成品庫。 這是有關應用程序防火牆和原理的知識來源:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.