php教程 php手册 php 防跨站攻击测试例子

php 防跨站攻击测试例子

May 25, 2016 pm 04:41 PM

跨站攻击就是利用程序上的一些细节或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不一样就可以提交重新输入提交数据了.


教程网址:

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

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)