Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set Cookie not working

Posted on 2007-03-31
6
Medium Priority
?
206 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

610 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