0

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>"; 
        }
4
  • 3
    In which line are you getting the error? Commented Dec 26, 2011 at 18:37
  • i solved it, thanks man it was coming from me formatting the time-stamp, i just always assumed that you were allowed to put any date in the time-stamp (formatted or not)... but apparently you cant ! :) Commented Dec 26, 2011 at 18:43
  • Why do you md5 encrypt the cookie data names? Commented Dec 26, 2011 at 18:50
  • just minor security attempts md5 the names so a asshole user would stop editing the cookie name with those hacker browser like maxithon..... were they aloow a user to minupulate the cookie data Commented Dec 26, 2011 at 20:01

2 Answers 2

4

$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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.