Solved

Cookies not Working (PHP)

Posted on 2011-09-14
4
230 Views
Last Modified: 2012-05-12
Hi, cookies just aren't biting on my system. I have tried several vanilla tests but they don't seem to be being created. And yes cookies are activated in my test browser (Opera), but don't work in Firefox or IE either.

Any ideas? Here's some sample test code that I have C&Ped staright off a tutorial site...

<!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>Cookie Test</title>
</head>
<body>
<?php
//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
?>
<?php
if(isset($_COOKIE['lastVisit']))
      $visit = $_COOKIE['lastVisit'];
else
      echo "You've got some stale cookies!<BR>";

echo "Your last visit was on: ". $visit;
?>
</body>
</html>

Excuse the W3 headers...
0
Comment
Question by:edhasted
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
h4hardy earned 500 total points
ID: 36535240
you have to make the Cokie code at the top of the page..

<?php
//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time();
$value= "hardik";
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths); 

?>
<!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>Cookie Test</title>
</head>
<body>

<?php
echo "<pre>";
print_r($_COOKIE);
if(isset($_COOKIE['lastVisit']))
      $visit = $_COOKIE['lastVisit'];
else
      echo "You've got some stale cookies!<BR>";

echo "Your last visit was on: ". $visit;
?>
</body>
</html>

Open in new window

0
 

Author Comment

by:edhasted
ID: 36535266
That worked! I had tried putting the sets at the top of the page before but this time it worked.
So do Cookie Sets always have to be at the top?

If so what is the approved method of how to write new values generated in a current page.
0
 
LVL 4

Expert Comment

by:h4hardy
ID: 36535272
yes a "Cookie Sets always have to be at the top" always.. and i m not getting with the statement "If so what is the approved method of how to write new values generated in a current page."
0
 

Author Comment

by:edhasted
ID: 36535298
Apologies.

I have a set of variables generated in a page by PHP.

In order to save these as cookies what's the best approach?
Make sure the variables are all created as sessions and then call up a intermediary/dummy page with the code at the top that stores them as cookes...

I can understand why the code has to go at the top. From a coding point of view it appears awkward.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question