?
Solved

Cookies not Working (PHP)

Posted on 2011-09-14
4
Medium Priority
?
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
h4hardy earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

752 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