时间:2021-07-01 10:21:17 帮助过:14人阅读
<span style="font-size: small;"><?php  
session_start();  
  
if ($_POST['submit'] == "go"){  
    //check token  
    if ($_POST['token'] == $_SESSION['token']){  
        //strip_tags  
        $name = strip_tags($_POST['name']);  
        $name = substr($name,0,40);  
        //clean out any potential hexadecimal characters  
        $name = cleanHex($name);  
        //continue processing....  
    }else{  
        //stop all processing! remote form posting attempt!  
    }  
}  
  
$token = md5(uniqid(rand(), true));  
$_SESSION['token']= $token;  
  
  
function cleanHex($input){  
    $clean = preg_replace("![\][xX]([A-Fa-f0-9]{1,3})!", "",$input);  
    return $clean;  
}  
?>  
  
  
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">  
<p><label for="name">Name</label>  
<input type="text" name="name" id="name" size="20" maxlength="40"/></p>  
<input type="hidden" name="token" value="<?php echo $token;?>"/>  
<p><input type="submit" name="submit" value="go"/></p>  
</form>   </span>这种技术是有效的,这是因为在 PHP 中会话数据无法在服务器之间迁移。即使有人获得了您的 PHP 源代码,将它转移到自己的服务器上,并向您的服务器提交信息,您的服务器接收的也只是空的或畸形的会话令牌和原来提供的表单令牌。它们不匹配,远程表单提交就失败了。