首頁 > php教程 > php手册 > 主體

php 防跨站攻击测试例子

WBOY
發布: 2016-05-25 16:41:40
原創
1357 人瀏覽過

跨站攻击就是利用程序上的一些细节或bug问题进行的了,那么我们要如何防止跨站攻击呢?下面我们费话不说多了来给大家整理一个防止跨站攻击例子,希望对各位有帮助.

php 防跨站攻击测试例子代码如下:

<?php
/** 
 * enc
 */
function encrypt($token_time) {
    return md5(&#39;!@##$@$$#%43&#39; . $token_time);
}
$token_time = time();
$token = encrypt($token_time);
$expire_time = 10;
if ($_POST) {
    $_token_time = $_POST[&#39;token_time&#39;];
    $_token = $_POST[&#39;token&#39;];
    if ((time() & ndash;
    $_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&#39; /> 
<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 存储到session更好,这儿只是简单示例下.

简单分析:token防攻击也叫作(令牌)了,我们在用户访问页面时就生成了一个随机的token保存session与表单了,用户提交时如果我们获取到的token与session不一样就可以提交重新输入提交数据了.


教程网址:

欢迎收藏∩_∩但请保留本文链接。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!