Jump to content

Help with setcookie function


zlatangu

Recommended Posts

I have a problem, each time that i reload the page i get a different $num variable why is that happening? Can somebody help me, i'm beginner in php.

<?php
if(isset($_COOKIE["nums"])){
	$num = $_COOKIE['nums'];
}else{
	$num = rand(1,9).date('Y').date('m').date('d').date('h').date('i').date('s');
	setcookie("nums",$num, 9999999999);  	
}  
  echo "You have  " .$num. "  ....";
?>
Link to comment
Share on other sites

are you getting a header error due to outputting something (or even your file having been saved with a byte order mark character) before the setcookie() statement?

 

having php's error_reporting/display_errors set to report and display all php errors, when debugging code problems, will help.

Link to comment
Share on other sites

The thing I noticed is your expiration time. It's a unix timestamp. If I enter

echo date('Y-m-d H:i:s', 9999999999);

I get 2014-09-06 21:50:07

 

 

Hmm...for some reason, I get "2286-11-20 09:46:39".

 

@zlatangu - The documentation for setcookie() recommends that you use the time() function when setting the expiration date. Here is an example provided in the manual:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
?>

 

Edited by cyberRobot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.