Solved

Cookies not Working (PHP)

Posted on 2011-09-14
4
229 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

770 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