creeper102 Posted May 14, 2010 Share Posted May 14, 2010 I have written the following code: <?php if(empty($HTTP_COOKIE_VARS["times"])) { $times=1; } else { $times=$HTTP_COOKIE_VARS["times"]; $times=$times+1; } setcookie("times", $times, time()+3600); echo "This is your $times visit within an hour"; ?> this code should preview like this: This is your 1 visit within an hour and when i will reload the page the '1' will change to '2' and then to '3' and so on. However when i reload the page it still remains '1' and doesnt show '2'. is anything wrong in this script? Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/ Share on other sites More sharing options...
cs.punk Posted May 14, 2010 Share Posted May 14, 2010 $HTTP_COOKIE_VARS is deprecated use $_COOKIE. Try it and see if it fixes your problem. Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058214 Share on other sites More sharing options...
creeper102 Posted May 14, 2010 Author Share Posted May 14, 2010 nope still no effect the output is still the same i will be very thankful for anyone who helps me solve this problem is there anything wrong in the code? Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058418 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2010 Share Posted May 14, 2010 This works for me: <?php if(empty($_COOKIE["times"])) { $times=1; } else { $times=$_COOKIE["times"] + 1; } setcookie("times", $times, time()+3600); echo "This is your $times visit within an hour"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058427 Share on other sites More sharing options...
creeper102 Posted May 14, 2010 Author Share Posted May 14, 2010 this code too doesnt work for me... :'( :'( :'( is there a problem with my browser then i.e. is it unable to store cookies? i tested this code in both IE and firefox, both negative results... please tell me guyz does this code work for you? Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058445 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2010 Share Posted May 14, 2010 When you test this in Firefox, go to "Page Info" under the "Tools" menu. Open the "Security" tab and look for "Is this web site storing information (cookies) on my computer?" That should show you the cookie & it's value. Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058459 Share on other sites More sharing options...
PFMaBiSmAd Posted May 14, 2010 Share Posted May 14, 2010 You are likely getting a header() error that is preventing setcookie() from setting the cookie. You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will both report and display all the errors it detects. Stop and start your web server to get any change made to php.ini to take effect and confirm that the settings were changed using a phpinfo() statement in case the php.ini that you are changing is not the one that php is using. Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058461 Share on other sites More sharing options...
creeper102 Posted May 15, 2010 Author Share Posted May 15, 2010 sorry i could'nt get what you are saying i am new to php i found that problem is with setcookie() it doesnt set the cookie can anybody suggest me anything or just show me a simple html code which uses php script to retrieve a cookie that has been set. thanx in advance Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058711 Share on other sites More sharing options...
PFMaBiSmAd Posted May 15, 2010 Share Posted May 15, 2010 For debugging purposes, add the following two lines of code immediately after the line with your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1058727 Share on other sites More sharing options...
creeper102 Posted May 16, 2010 Author Share Posted May 16, 2010 This is my full code that i've written <?php ini_set("display_errors", "1"); error_reporting(E_ALL); if(empty($HTTP_COOKIE_VARS['times'])) { $times=1; } else { $times=$HTTP_COOKIE_VARS["times"]; $times=$times+1; } setcookie("times", "$times", time()+3600); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p> </p> This is your <?php echo "$times"; ?> visit <p> </p> </body> </html> I also added the two lines which you asked me to add it desplays this result: This is your 1 visit even after refreshing same result is shown. Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1059038 Share on other sites More sharing options...
PFMaBiSmAd Posted May 16, 2010 Share Posted May 16, 2010 Two days ago, you were told that $HTTP_COOKIE_VARS was depreciated and to use and were also shown code that used $_COOKIE instead. Those two different people that provided that information did not do so because they needed practice typing. They did so because it was information you needed to follow. You are not getting any $HTTP_COOKIE_VARS related error reported/displayed in your current code because empty() on a nonexistent variable does not produce an error. However, the code you posted DOES have a space before the <?php tag which would be producing a php error for that code, so I will guess that you are attempting to execute this code on a server where ini_set() has been disabled and the line of code that should be setting display_errors so that you would see any php detected errors is not doing anything. Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1059087 Share on other sites More sharing options...
creeper102 Posted May 16, 2010 Author Share Posted May 16, 2010 thnxxxxxxxxxxxxxxxxxxxx very much PFMaBiSmAd the problem was with spacing my code had a space before <?php soory for any trouble caused ;D ;D ;D Quote Link to comment https://forums.phpfreaks.com/topic/201733-simple-coding-problem-with-http_cookie_vars/#findComment-1059190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.