好吧,所以我已经学会了一点PHP,并尝试做一个简单的应用程序,但我不知道是我的网页安全从xss和其他此类攻击。如何保护网站免受xss(跨网站脚本)
我的PHP代码
<?php
$title=$keywords=$description="";
$valid_er="";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST['title'])){
$valid_er="has-error";
}
else{
$title="<title>".test($_POST["title"])."<title>";
}
$keywords='<meta name="keywords" content="'.test($_POST["keywords"]).'" />';
$description='<meta name="description" content="'.test($_POST['description']).'" />';
}
function test($ci){
$ci=htmlentities($ci);
$ci=stripcslashes($ci);
return $ci;
}
?>
和我的HTML表单
<form method='post' class='form-group' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label> Your Title </label> <input placeholder="Your websites title" type="text" name="title" class='form-control' class='form-group-item'/></br>
<label> Keywords </label> <input placeholder="Your keywords separated by comma " type="text" name="keywords" class='form-control' class='form-group-item'/></br>
<label>Description </label> <textarea placeholder="A nice description about your website;" name="description" class='form-control'></textarea></br>
<input type="submit" class='btn btn-info'>
</form>
我只是想知道我是不是容易受到跨站点脚本,因为我不认为只有使用
htmlspecialchars()
会保护我。
请参阅http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php – christophetd
如果你能告诉我是我的代码安全,这将是非常有益的 –
你不需要使用stripcslashes来转义HTML。 htmlspecialchars就够了。此外,当您在PHP代码中编写HTML时,不应使用转义字符,如'<',因为它们将被打印在浏览器中而不是被解释。基本上:当你写HTML的时候,一般写下来;当您显示来自用户输入的内容时,请使用htmlspecialchars。这有帮助吗? – christophetd