Solved

Cookies not Working (PHP)

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now