You are invited to Log in or Register a free Frihost Account!

PHP Cookies - Problems

if($_POST['remember']) {
$passwordcook = sha1(md5(md5(sha1(md5(sha1(sha1(md5($user[password]))))))));
setcookie("id", $user['id'], time()-3600, "", "");
setcookie("password", $passwordcook, time()-3600, "", "");

I get the headers already sent error message which really gets on my nerves. I downloaded a Cookie Editor to check if the cookie has been set and it has not. The line says it is the two lines to set the cookies. So what's up with my code?

NOTE: the $user variable is defined above the if statement. I am using Sessions if that would make a difference. I don't really want to change my whole entire code to cookies, I just want the cookies to set if the user checked "remember me!" and if s/he did then it will automatically login for the user.

Kind Regards!
try this

setcookie("id", $user['id'], time()+3600, "/", ""); //Observe time()+3600 instead of time()-3600

Also, regarding your "headers already sent" issue, you have to set cookies before any output -- either php or html (check for any new lines or blank spaces in the html portion of your php - they matter).
Setting the expire to be time()+3600 would mean it would expire in an hour, I want it to last forever until the user clicks "Logout."

Also, what do you mean by any output? Or new lines? :S

Kind Regards.
You can't set it foreva. Just put in like 365x24x60x60 What is a year.
Okay I set it to time()+365*24*60*60 and I still get the error. Would I need to put ob_start at the beginning of my file?
You must make sure that you don't print any text before the cookie is set. Make sure there is no characters before your <? They is also outputted and make the header to be sent.
Diablosblizz wrote:
Setting the expire to be time()+3600 would mean it would expire in an hour, I want it to last forever until the user clicks "Logout."

Also, what do you mean by any output? Or new lines? :S

Kind Regards.

Output is any html data that is sent to browser. It could be in two forms. One is direct html in php file, other echo statements in php (or using any commands which send output to browser).


<html>    <-- html output
<body>   <-- html output
some html here  <-- html output
   <-- blank html output

//php here
echo "some data"; //<-- html output in php

html continues...

The best thing to do is start <?php tag at the first line, do all your processing and setcookie, only then use echo statement or html to print any html data.
I still don't quite understand, so here is my file:

I could post my whole code, but I don't want to spam. Razz
I will make it simple. You cannot have any echo statements before setcookie statement.

Put your setcookie before the first echo statement in your code and you will get rid of "headers already sent" error.
Okay, well this got rid of the error messages, but doesn't set the cookie. Or so I think... I'm currently at school, running Firefox on my USB drive!!! Very Happy, and I will edit the file some more when I get home.

Kind regards, and thanks for the support.
and you need you ob_start at the top as well for the cookies
Finally success! It was all because of the ob_start();, but because I had session_start(); I thought that it would cause problems to the two.

EDIT: Okay, I can't seem to get the cookie check to work:

if($logged['id']) {
   echo "<meta http-equiv='Refresh' content='2; URL=members/login.php'/>";
} if(isset($_COOKIE["idhotel"])) {
   echo "Sweet!";
} else {

It just goes to the else, but it works on the separate page with nothing else. There are no errors, and there is a ob_start();.

Any ideas now? And I have put the cookie check above the logged id and no change.

EDIT EDIT: It says that it is set on my custom cookie checker page, but not on the other one that I need it to be on. One says it has set and one says it's not. Why is it doing this? :S
If you are setting the cookie and checking for it on the same page, it does not work. You cannot access the cookie on the same page (actually, in the same http request) where you set it, but it will be available in the next page you fetch.
I got it working thanks to DanielXP over MSN.

Thanks! This can officially be closed now.
Related topics
PHP Cookies Not Working
PHP Tutorials
[php/cookies] poll on two sites
php upload problems
Question about a php and mysql script.
PHP 5 problems...
Disclaimer Redirection with cookies/sessions?
PHP Security problems with latest frihost changes
Cookies - Sessions
managing a bilingual site
CSS/Layout Help Please!
Is it a bug?
Any suggestion on add-on services that work with Frihost?
Is this dangerous for my web server???
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.