setting cookie through foreach loop inside while loop

非 Y 不嫁゛ 提交于 2021-02-11 14:15:57

问题


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>"; 
        }

回答1:


$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);

http://us3.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.




回答2:


The 3rd parameter "expire" of setCookie() expects an integer but you are proving date string. This is a Unix timestamp so is in number of seconds since the epoch. So, comment //$expire = date("Y-m-d h:i:s",$expire);

It wil then work fine.



来源:https://stackoverflow.com/questions/8638241/setting-cookie-through-foreach-loop-inside-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!