Jump to content

Simple Coding Problem with $HTTP_COOKIE_VARS


creeper102

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

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.