2个计数后,cookie计数器急剧增加

时间:2018-08-11 19:03:47

标签: php cookies counter

我在服务器中有此php代码。它接收发布请求并处理数据。如果计数器没有cookie,它还会设置一个cookie,并计算访问次数。我的问题是2次访问后,Cookie计数器值发生了巨大变化。它从2到1534014090。似乎我的Cookie计数器变量的值不保存其数据。它从文本文件读取用户名和密码。我的问题仅出现在第一个if语句中

<?php
setcookie("User", "Razine Bensari");
 $username = $_POST["username"];
 $password = $_POST['password'];
 $done = "The data has not been sent";
 $credsArray;
 $counter = 0;

 //check if cookie exists
if(isset($_COOKIE["counter"])){
  echo "<h1> Welcome Back ".$_COOKIE["User"] ." - " .$username."!!!</h1>";
  $cookieCount = ++$_COOKIE["counter"];
  setcookie('counter', $cookieCount + time() + 60);
  echo "<br> You have visited this page " .$_COOKIE["counter"]. " times";
} else { // starts counting if new visitor
  $cookieCount = 1;
  setcookie('counter', $cookieCount, time() + 60);
  echo "<h1> Welcome new Guest! </h1>";

}
//check if correctly received data through post
  if($username || $password){
    //Note, the validation has already been done in front end (html and javascript)
    $done = "The data has been sent to canvas.php file";
    echo "<h1> <?php $done ?></h1>";
  }
  //open the file that containes the credential to verify the password and username
  $file_key = fopen("key.txt", "r");
  echo "<br>I am now opening the file text...";

  //Even indexes are username, odd indexes are password because of the way the key.txt is set up (first line is usernmae, second line is password - this is one set of crednetials)
  if($file_key){
    while(!feof($file_key)){
      $credsArray[$counter] = fgets($file_key);
      $counter++;
      echo " reading the file... <br>";
    }
    echo "<br> This is the accepted username: <strong>$credsArray[0]</strong>, this is the corresponding password: <strong>$credsArray[1]</strong> <br>";
    echo "The username used is $username, the password used is $password. <br>";
    if(((String)$username == trim((String)$credsArray[0])) && ((String)$password == trim((String)$credsArray[1]))){
      session_start();
      $_SESSION['FirstLogin'] = "true";

      echo  "<h3> <br>You have correctly logged in!!! <br></h3>";
    } else {
      echo "<h4> Incorrect username or password</h4>";
    }
    fclose($file_key);
  } else {
    echo "error openeing the file";
  }
?>

1 个答案:

答案 0 :(得分:0)

setcookie('counter', $cookieCount + time() + 60); 

您在加值时间上缺少逗号(,)

setcookie('counter', $cookieCount, time() + 60);

// http://php.net/manual/en/function.setcookie.php
// setcookie("TestCookie", $value, time()+3600);