Solved

Set Cookie not working

Posted on 2007-03-31
6
199 Views
Last Modified: 2008-02-01
I created a test page at http://www.cmga.us/test2.php with this code...

-----------------
<?PHP
      print_r($_COOKIE);
      if ( !setcookie("asrf", "asdf", 0) )
            echo '-failed!';
?>
-----------------

It all ways fails... why?
0
Comment
Question by:JoshWegener
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:geoffreyreemer
ID: 18828148
The third variable in a cookie is a time variable. In your example, you are setting a cookie for exactly 0 seconds, which isn't very much of course. Try using the value 3600 there (= 1 hour) and see how it goes then.
0
 
LVL 4

Expert Comment

by:secondv
ID: 18828261
^ exactly.

bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly]]]]]] )

http://php.net/setcookie
0
 
LVL 1

Author Comment

by:JoshWegener
ID: 18828315
Well, I changed that, but it still fails
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:secondv
ID: 18828318
Move:

print_r($_COOKIE);

to AFTER trying to set a cookie.
0
 
LVL 1

Author Comment

by:JoshWegener
ID: 18828416
it prints out FAILED when it does not work... there is no need to move that. You can also just hit refresh.... because if it did store it in a cookie, it would display the value.
0
 
LVL 5

Accepted Solution

by:
geoffreyreemer earned 500 total points
ID: 18868846
Allright mate, I did some tests for you and found out some stuff. This is the right code:

<?php
   if (!setcookie("asrf", "asdf", time()+3600) )
      echo '-failed!';
   print_r($_COOKIE);
?>

Why is this right?

1. It seems you have to add the seconds to the current time when you set a cookie. So it's not just 3600, but time()+3600.
2. You mustn't set a cookie after something has been outputted. Cookies can only be set if there haven't been echo's, prints or other output things. In fact, I even got a genuine "headers already sent" error.

So this will work for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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

18 Experts available now in Live!

Get 1:1 Help Now