1. 程式人生 > >php防止惡意重新整理與刷票的方法

php防止惡意重新整理與刷票的方法

<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通過session禁止頁面重新整理</title>
<style type="text/css">
<!--
.style1 {
font-size: 14px;
font-family: "華文仿宋";
font-weight: bold;
}
.style2 {font-family: "華文琥珀"}
-->
</style>
</head>
<body>
<?php
//使用文字儲存資料
   if($_SESSION[temp]==""){
   if(($fp=fopen("counter.txt","r"))==false){ 
   echo "開啟檔案失敗!";
     }else{ $counter=fgets($fp,1024);   //讀取檔案中資料
     fclose($fp);                        //關閉文字檔案
     $counter++;                         //計數器增加1
     $fp=fopen("counter.txt","w");       //以寫的方式開啟文字檔案
  
     fputs($fp,$counter);                //將新的統計資料增加1
     fclose($fp);    }                   //關閉文
//從文字檔案中讀取統計資料
       if(($fp=fopen("counter.txt","r"))==false){
       echo "開啟檔案失敗!";
       }else{
        $counter=fgets($fp,1024);
        fclose($fp);
           echo "數字計數器: " .$counter ;
    }   //輸出訪問次數
     $_SESSION[temp]=1; //登入以後,$_SESSION[temp]的值不為空,給$_SESSION[temp]賦一個值1
     }else{
     echo "<script>alert('您不可以重新整理本頁!!'); history.back();</script>";
     }
?>
<table width="300" border="0" cellpadding="0" cellspacing="0" background="images/141.jpg">
<tr>
    <td height="35" align="center"><span class="style1">通過session禁止頁面重新整理</span></td>
</tr>
<tr>
    <td height="40" align="center"><span class="style2">
      <?php if(($fp=fopen("counter.txt","r"))==false){
      echo "開啟檔案失敗!";
      }else{
        $counter=fgets($fp,1024);
        fclose($fp);
           echo "網頁訪問量: " .$counter ; 
    }   //輸出訪問次數
 ?>
    </span></td>
</tr>
<tr>
    <td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>