Solved

Set Cookie not working

Posted on 2007-03-31
6
200 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 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