[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Cookies not Working (PHP)

Posted on 2011-09-14
4
Medium Priority
?
237 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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

650 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