doutou6803 2018-08-11 19:03
浏览 69

cookie计数器在2次计数后急剧增加

I have this php code in a servers. It receives a post request and deals with the data. It also sets a cookie if the counter there is no cookie, and counts the number of visits. My problem is that after 2 visits, the cookie counter value changes dramatically. it goes from 2 to 1534014090. It seems the value of my cookie counter variable does not hold its data. It reads the username and password from a text file. My problem occurs only in the first if statement

<?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条回答 默认 最新

  • dpsu84620 2018-08-11 19:54
    关注
    setcookie('counter', $cookieCount + time() + 60); 
    

    you missing comma (,) on value you adding time

    setcookie('counter', $cookieCount, time() + 60);
    
    // http://php.net/manual/en/function.setcookie.php
    // setcookie("TestCookie", $value, time()+3600); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作