php跨站攻击实例分析

yipeiwu_com6年前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 存储到session更好,这里只是简单示例下

简单分析:

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

希望本文所述对大家的php程序设计有所帮助。

相关文章

php array_slice函数的使用以及参数详解

复制代码 代码如下:<?php /* 用手册上的例子 */ $input = array ("a", "b", "c", "d", "e"); $output = array_sl...

php递归删除目录与文件的方法

本文实例讲述了php递归删除目录与文件的方法。分享给大家供大家参考。具体实现方法如下: <?php function deldir($path){ $dh = ope...

教大家制作简单的php日历

教大家制作简单的php日历

最近的一个项目中,需要将数据用日历方式显示,网上有很多的JS插件,后面为了自己能有更大的控制权,决定自己制作一个日历显示。如下图所示: 一、计算数据 1、new一个Calendar类...

php join函数应用

复制代码 代码如下: $key = array(); $val = array(); foreach ($_POST as $k=>$v) { $key[] = '`'.strip...

PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法

本文实例讲述了PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法。分享给大家供大家参考。具体分析如下: PHP 连接 MSSQL 的新手经常遇到这个问题:数据库里面的...