这篇文章主要为大家详细介绍了php跨站攻击的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
本文实例讲述了php跨站攻击的原理与防范技巧。分享给大家供大家参考。具体方法分析如下:
跨站攻击就是利用程序上的一些细节或bug问题进行的,那么我们要如何耿防止跨站攻击呢?下面就以一个防止跨站攻击例子来说明,希望对各位有帮助。
代码如下:
<?php
#demo for prevent csrf
/**
* enc
*/
function encrypt($token_time) {
return md5('!@##$@$$#%43' . $token_time);
}
$token_time = time();
$token = encrypt($token_time);
$expire_time = 10;
if ($_POST) {
$_token_time = $_POST['token_time'];
$_token = $_POST['token'];
if ((time() – $_token_time) > $expire_time) {
echo “expired token”;
echo “<br />”;
}
echo $_token;
echo “<br />”;
$_token_real = encrypt($_token_time);
echo $_token_real;
//compare $_token and $_token_real
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
<title>test for csrf</title>
<meta http-equiv=”" content=”" />
</head>
<body>
<form method=”post” action=”">
<input type=”text” name=”text” id=”" value=”hello” />
<input type=”hidden” name=”token” id=”" value=”<?php echo $token ?>” />
<input type=”hidden” name=”token_time” id=”" value=”<?php echo $token_time ?>” />
<input type=”submit” name=”submit” id=”" value=”submit” />
</form>
</body>
</html>
token防攻击也叫作令牌,我们在用户访问页面时就生成了一个随机的token保存session与表单了,用户提交时如果我们获取到的token与session不一样就可以提交重新输入提交数据了
希望本文所述对大家的php程序设计有所帮助。
本文来自:http://www.q1010.com/173/17876-0.html
注:关于php跨站攻击的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。