Home > php教程 > php手册 > PHP检查表单提交是否来自于本站

PHP检查表单提交是否来自于本站

WBOY
Release: 2016-06-13 11:35:32
Original
872 people have browsed it

方法一:
你可以把处理提交数据的代码写到一个单独的文件里,比如form.php。   
     if   (defined(’INSIDE’))   {//判断是否有定义INSIDE常量   
  //处理表单   
  }   else   {   
  exit(’错误’);   
  }   
  ?>   
    
  然后,在提交表单的那个页面先定义INSIDE这个常量,常量值无所谓   
  define(’INSIDE’,   ’TRUE’);   
    
  接着,判断是否有提交,如果提交,则   
  include   ’form.php’;

======================================================================================= 方法二:

if(PHP_VERSION if(isset($_SERVER["HTTP_REFERER"])){ 
//针对部分浏览器可能无HTTP_REFERER,所以做这么一个判断 
    $servername=$_SERVER[’SERVER_NAME’]; 
    $sub_from=$_SERVER["HTTP_REFERER"]; 
    $sub_len=strlen($servername); 
    $checkfrom=substr($sub_from,7,$sub_len); 
    if($checkfrom!=$servername) 
    { 
        echo "<script>window.alert(&rsquo;数据来源有误,本站不接收站外提交的数据!&rsquo;);window.opener=self;window.close();</script>"; 
        exit; 
    } 

?> 
    此方法并非绝对安全,因为HTTP_REFERER是可以伪造的,只能是防君子但不能防小人了,但在一般情况下已经够用。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template