2016-05-22 46 views
0

如何限制用戶在一分鐘內完成的操作? 我有這個PHP代碼在PHP中放置限制

if(isset($_POST['new_post'])){ 
      $Text=htmlspecialchars($_POST['new_post'],ENT_QUOTES); 
      $Text=trim($Text); 
      if (is_uploaded_file($_FILES['Upload_f']['tmp_name'])) { 
        $fileP=$_FILES['Upload_f']; 
        $fileP_name=$fileP['name']; 
        $fileP_tmp=$fileP['tmp_name']; 
        $fileP_size=$fileP['size']; 
        $fileP_error=$fileP['error']; 
        $fileP_extension=explode('.', $fileP_name); 
        $fileP_extension=strtolower(end($fileP_extension)); 
        $allowed=array('jpg','png'); 
        if (in_array($fileP_extension, $allowed)){ 
         if ($fileP_error===0) { 
          if ($fileP_size<=2097152){ 
           $fileP_new_name=uniqid().'.'.$fileP_extension; 
          } 
         } 
         $NotInarray=false; 
        }else{ 
         $fileP_new_name=""; 
         $NotInarray=true; 
        } 
        $Fileuploaded=true; 
      }else{ 
       $fileP_new_name=""; 
       $fileP=0; 
       $Fileuploaded=false; 
       $NotInarray=false; 
      } 
       $Posts=$con->query("SELECT Posts FROM user_opt WHERE Username='$NameId'"); 
       $row=$Posts->fetch_row(); 
      if (strlen($Text)>400) { 
       $Res="Error occurred.Please try again"; 
       $PostNum=$row[0]; 
      }elseif(strlen($Text)==0 && $fileP==0){ 
       $Res="Both fields are empty"; 
       $PostNum=$row[0]; 
      }elseif($Fileuploaded===true){ 
       if ($NotInarray==true) { 
       $Res="Only jpg and png files are allowed"; 
       $PostNum=$row[0]; 
       }elseif ($fileP_error!=0) { 
       $Res="Error occurred.Please try again"; 
       $PostNum=$row[0]; 
       }else{ 
        $Res="Success"; 
        $PostNum=$row[0]+1; 
        $upladed++; 
       } 
      }else{ 
        function generateRandomString($length) { 
         $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
         $charactersLength = strlen($characters); 
         $randomString = ''; 
         for ($i = 0; $i < $length; $i++) { 
          $randomString .= $characters[rand(0, $charactersLength - 1)]; 
         } 
         return $randomString; 
        } 
        $Rand=generateRandomString(100); 
        $query=$con->query("INSERT INTO uploads (Rand,Username,image,`Text`,`Date`) VALUES('$Rand','$NameId','$fileP_new_name','$Text',NOW())"); 
        $querya=$con->query("UPDATE user_opt SET posts=posts+1 WHERE Username='$NameId'"); 
        $PostNum=$row[0]+1; 
        $Res="Success";   
        $upladed++; 
       } 
       echo json_encode(array($Res,$PostNum,$upladed)); 
     } 

此代碼是由ajax.How叫我可以使此代碼執行最多5次一分鐘?我試圖使它這樣

$upladed=0; 
    if(isset($_POST['new_post'])){ 
      if ($upladed<=5) { 
       code 
    } 
} 

但每次新的AJAX來$上移成爲0再次

+0

您需要將變量存儲在某個地方,以便能夠跨請求,會話,緩存等使用它。 – JimL

+0

@JimL如何將它存儲在緩存中? –

回答

1

您可能希望使用會話來存儲$上傳的變量的值,並在每次發生Ajax調用時檢索它。您也可能希望在第一篇文章發生時存儲實際的分鐘數,以確保在那一分鐘內不會有更多文章到達。當另一分鐘啓動時,您應該使上傳的$無效(將其設置爲0)。

更多更好,如果你是存儲後的第一個實際的第二次,並給予60秒的其他潛在上傳(到你的極限)

修改後的代碼看起來是這樣的:

<?php 

session_start(); 

function generateRandomString($length) { 
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $charactersLength = strlen($characters); 
    $randomString = ''; 
    for ($i = 0; $i < $length; $i++) { 
     $randomString .= $characters[rand(0, $charactersLength - 1)]; 
    } 
    return $randomString; 
} 

function isFileUploadAllowed() { 
    $isAllowed     = true; 
    $timeNow     = time(); 
    $timeFrameInSeconds   = 60; 
    $maxUploadsInTimeFrame  = 5; 
    $firstUploadTime   = $_SESSION['firstUploadTime'] ? intval($_SESSION['firstUploadTime']) : $timeNow; 
    $numberOfUploadsInTimeFrame = $_SESSION['numberOfUploadsInTimeFrame'] ? intval($_SESSION['numberOfUploadsInTimeFrame']) : 0; 
    $givenTimeFrameExpired  = (($firstUploadTime + $timeFrameInSeconds) < $timeNow); 

    // when there would be more time to allow upload 
    if (!$givenTimeFrameExpired) { 
     // disallowing only when the limit is reached 
     if ($numberOfUploadsInTimeFrame + 1 > $maxUploadsInTimeFrame) { 
      $isAllowed = false; 
     } 
    } 

    // if there is no need to restrict this upload 
    if ($isAllowed === true) { 
     // if previous time frame is expired, reset 'firstUploadTime' and 'numberOfUploadsInTimeFrame' 
     if ($givenTimeFrameExpired) { 
      $_SESSION['firstUploadTime'] = $timeNow; 
      $_SESSION['numberOfUploadsInTimeFrame'] = 0; 
     } 

     // increasing the number of uploaded files 
     $_SESSION['numberOfUploadsInTimeFrame']++; 
    } 

    return $isAllowed; 
} 

if(isset($_POST['new_post'])){ 
    $Text=htmlspecialchars($_POST['new_post'],ENT_QUOTES); 
    $Text=trim($Text); 
    if (is_uploaded_file($_FILES['Upload_f']['tmp_name'])) { 
     if (isFileUploadAllowed()) { 
      $fileP=$_FILES['Upload_f']; 
      $fileP_name=$fileP['name']; 
      $fileP_tmp=$fileP['tmp_name']; 
      $fileP_size=$fileP['size']; 
      $fileP_error=$fileP['error']; 
      $fileP_extension=explode('.', $fileP_name); 
      $fileP_extension=strtolower(end($fileP_extension)); 
      $allowed=array('jpg','png'); 
      if (in_array($fileP_extension, $allowed)){ 
       if ($fileP_error===0) { 
        if ($fileP_size<=2097152){ 
         $fileP_new_name=uniqid().'.'.$fileP_extension; 
        } 
       } 
       $NotInarray=false; 
      }else{ 
       $fileP_new_name=""; 
       $NotInarray=true; 
      } 
      $Fileuploaded=true; 
     } 
    }else{ 
     $fileP_new_name=""; 
     $fileP=0; 
     $Fileuploaded=false; 
     $NotInarray=false; 
    } 
     $Posts=$con->query("SELECT Posts FROM user_opt WHERE Username='$NameId'"); 
     $row=$Posts->fetch_row(); 
    if (strlen($Text)>400) { 
     $Res="Error occurred.Please try again"; 
     $PostNum=$row[0]; 
    }elseif(strlen($Text)==0 && $fileP==0){ 
     $Res="Both fields are empty"; 
     $PostNum=$row[0]; 
    }elseif($Fileuploaded===true){ 
     if ($NotInarray==true) { 
     $Res="Only jpg and png files are allowed"; 
     $PostNum=$row[0]; 
     }elseif ($fileP_error!=0) { 
     $Res="Error occurred.Please try again"; 
     $PostNum=$row[0]; 
     }else{ 
      $Res="Success"; 
      $PostNum=$row[0]+1; 
      $upladed++; 
     } 
    }else{ 
      $Rand=generateRandomString(100); 
      $query=$con->query("INSERT INTO uploads (Rand,Username,image,`Text`,`Date`) VALUES('$Rand','$NameId','$fileP_new_name','$Text',NOW())"); 
      $querya=$con->query("UPDATE user_opt SET posts=posts+1 WHERE Username='$NameId'"); 
      $PostNum=$row[0]+1; 
      $Res="Success";   
      $upladed++; 
     } 
     echo json_encode(array($Res,$PostNum,$upladed)); 
} 
+0

我如何在PHP中存儲秒? –

+0

所以你可以用time()函數得到它。它以秒爲單位給出當前時間。另存爲:$ _SESSION ['firstUploadTime'] = time();在將它存儲到會話中之後,您可以將其與新的ajax調用的實際時間進行比較。 – SalDev

+0

你可以在你的答案中寫下代碼我不知道你在說什麼 –