Search code examples
phpwhile-loopsetcookie

setting cookie through foreach loop inside while loop


I am getting an error while trying to set these cookies through a foreach loop inside a while loop. The error I am getting is .... Notice: A non well formed numeric value encountered in
php script:

while($row = mysql_fetch_array($sql)){
            $path = "/";
            $expire =  time() + 2592000;
            $expire =  date("Y-m-d h:i:s",$expire);
            $c = array(
            md5('id')=>$row['id'],
            md5('name')=>$row['u'],
            md5('sex')=>$row['s'],
            md5('country')=>$row['co'],
            md5('state')=>$row['st'],
            md5('city')=>$row['ci'],
            md5('timezone')=>$row['ti']
            );
            foreach($c as $name=>$value){
                setcookie($name,$value,$expire,$path);
            }
            echo "Logging you in! <img src=\"source/image/50gl.gif\"><br>"; 
        }

Solution

  • $expire is expected to be an int. You have a string. This line is unnecessary, and the cause of the problem:

    $expire =  date("Y-m-d h:i:s",$expire);
    

    https://www.php.net/setcookie

    You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.