所以基本上有人告訴我我的網站很容易受到XSS攻擊,所以我正在努力解決這個問題。我被告知htmlspecialchars方法是防止這種情況的好方法。我用這段代碼成功地阻止了XSS攻擊嗎?
我做了這個功能
function _e($string){
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
,我使用它,例如下面
$userName = _e($_POST['userName']);
$Pass = _e($_POST['password']);
問題:
1)僅基於輸入XSS攻擊,或我是否需要對警報和回顯語句執行此操作?
2)我有沒有成功地停在下面的頁面XSS攻擊**
<?php
session_start();
if(isset($_SESSION['user_id'])){
header("Location: index.php");
}
function _e($string){
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
include '../includes/connection.php';
$userName = _e($_POST['userName']);
$Pass = _e($_POST['password']);
if(!empty($userName) && !empty($Pass)){
$sql = "SELECT * FROM Admins WHERE Username='$userName'";
$sqlr = mysqli_query($connect,$sql);
$sqlrow = $sqlr->fetch_assoc();
$dbPass = $sqlrow['Password'];
$hash = password_verify($Pass, $dbPass);
if ($hash == 0){
die("There was no password found matching what you have entered.");
}else{
$records = "SELECT * FROM Admins WHERE Username='$userName' AND Password='$dbPass' AND AdminLevel >=1";
$results = mysqli_query($connect,$records);
if ($results->num_rows == 1){
$row = $results->fetch_assoc();
$_SESSION['user_id'] = $row['ID'];
$_SESSION['admin_level'] = $row['AdminLevel'];
$_SESSION['user_name'] = $row['Username'];
$easyName = $_SESSION['user_name'];
$recordsS = "UPDATE `Admins` SET Status='1' WHERE Username='$userName'";
$resultsS = mysqli_query($connect,$recordsS);
header("Location: index.php");
}else{
$message = "Either you have entered the incorrect login information, or you account has not been approved yet.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ServerSide Moderation Services</title>
<link rel="stylesheet" href="../styles/mainStyles.css" type="text/css" />
<link rel="stylesheet" href="../styles/loginFormStyle.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body class="body">
<?php include '../includes/header.php'; ?>
<div class="mainContent">
<div class="logRegArea">
<article class="leftContent">
<header>
<h2 class="loginArea" style="text-align:center">Login Below:</h2>
</header>
<content>
<div id="login">
<form action="../pages/login.php" method="POST">
<input type="text" placeholder="Enter Your Username" name="userName">
<input type="password" placeholder="Enter Your Password" name="password">
<input type="submit">
</form>
</div>
</content>
</article>
</div>
</div>
<footer class="mainFooter">
<p>This website was developed by ROBLOX user: <a href="https://www.roblox.com/users/8869935/profile" title="Made by: wattleman">wattleman</a></p>
</footer>
</body>
</html>
請使用PHP的哈希函數來設置密碼。切勿存儲純文本密碼! –