Solved

Set Cookie not working

Posted on 2007-03-31
6
196 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
Comment Utility
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
Comment Utility
^ 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
Comment Utility
Well, I changed that, but it still fails
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:secondv
Comment Utility
Move:

print_r($_COOKIE);

to AFTER trying to set a cookie.
0
 
LVL 1

Author Comment

by:JoshWegener
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

12 Experts available now in Live!

Get 1:1 Help Now