Page 1 of 1

Help with: setcookie

Posted: Sat Jan 23, 2010 5:53 pm
by Marbled-D
Hey! I've got a problem. This works perfectly on my localhost. But not on my one.com server... Why??

Code: Select all

if (isset($_REQUEST['voted']))
{
    if(isset($_COOKIE["Mysite"])) 
    { 
         echo "<br /><p>You can just vote once.</p>"; 
    } 
    else 
    { 
         $month = 2592000 + time();
         echo $month;
         setcookie('Mysite'.$id, 'Somevaule', $month);
         $identity = $_REQUEST['id'];
         mysql_query("UPDATE vote SET votes = votes+1 WHERE id = $identity"); 
         echo "<br /><p>Thank's for your vote!</p>"; 
    } 
}
After hours of trying to find the problem I think it has something to do with the setcookie. I can not find any in my temporary internet files.
Please Help! It's probably easy for you...

Re: Help with: setcookie

Posted: Sun Jan 24, 2010 4:19 am
by social_experiment
http://www.php.net/manual/en/function.setcookie.php

Read there for more about setcookie(). Do you receive any error messages?

Re: Help with: setcookie

Posted: Sun Jan 24, 2010 6:35 am
by Apollo
Here's your problem:
Marbled-D wrote:echo $month;
setcookie('Mysite'.$id, 'Somevaule', $month);
First you echo, then you set cookie? Won't work. Cookies are HTTP headers, which have to be outputted *before* any regular output.

Re: Help with: setcookie

Posted: Sun Jan 24, 2010 12:21 pm
by Marbled-D
Hey!

Still can't figure it out...

I don't get any error-masseges. My text "Thank's for your vote!" just pops up, but when i look in the cookies-directory in my browser, there is no cookie!
I tested it on my localhost again (Wamp) and it still works fine there.

To get rid of echo $month; didn't help, unforunatley. I've tried to read at php.net, and tried alot. But nothing helps.

I've tried

Code: Select all

setcookie('Mysite', 'Value', time()+3600*24*1000, "/", ".mysite.com", 0);

and a lot more but if I do a check to see if the cookie is set, I get no massege.. ( I tried

Code: Select all

$didItWork = setcookie('Mysite', 'Value', time()+3600*24*1000);
if($didItWork) {echo "Yipiii";}
but there is no Yipiii on my page.) :(

What could the problem be???

Please help!!

Re: Help with: setcookie

Posted: Sun Jan 24, 2010 3:24 pm
by social_experiment
Try the code below, create a new php document and see if it works. I tried it and found that i had a setting in FF which clears all history when i close my browser. Maybe a similar setting is the problem?

Code: Select all

 
<?php
    if (isset($_COOKIE['demo'])) {
        echo $_COOKIE['demo'];
    }
    else {
        setcookie("demo", "arbValue", time()+3600, "/");
    }
?>
 

Re: Help with: setcookie

Posted: Sun Jan 24, 2010 5:58 pm
by Marbled-D
Oooh, Yeah!

That worked... sort of...

I tried to put in the rest of my page above the thing social_experiment wrote, but then it didn't work. So I guess there is some problem with cookies and the declaration in the beginning of the page...

Code: Select all

<?php
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"sv\" lang=\"sv\">";
?>
<head>
<title>MySite</title>
</head>
<body>
<?php
    if (isset($_COOKIE['huru'])) {
        echo $_COOKIE['huru'];
    }
    else {
        setcookie("huru", "Mamma", time()+3600, "/");
    }
?>
</body>
</html>
This doesn't work!!! I guess the problem is expained here: http://www.php.net/manual/en/function.s ... .php#78833 but I don't really get what I have to do to get around this problem..?

/Thank's a lot for the help so far... But I need more! =)

Re: Help with: setcookie

Posted: Mon Jan 25, 2010 12:13 am
by social_experiment

Code: Select all

 
<?php
    ob_start();
   echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
    <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"sv\" lang=\"sv\">";
 ?>
 <head>
 <title>MySite</title>
 </head>
 <body>
 <?php
     if (isset($_COOKIE['huru'])) {
         echo $_COOKIE['huru'];
     }
     else {
         setcookie("huru", "Mamma", time()+3600, "/");
    }
?>
</body>
</html>
<?php
    ob_end_flush();
?>
 
I turned on the output buffering and stopped it again at the end of the script and the cookie was set. Are you trying to place this code inside an html page or an xml page? Im asking because of the

Code: Select all

 
<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
 
inside the head of your document.

Re: Help with: setcookie

Posted: Mon Jan 25, 2010 2:45 am
by Apollo
About that last code:
Marbled-D wrote:

Code: Select all

<?php
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
.....
.....
setcookie("huru", "Mamma", time()+3600, "/");
Remember:
Apollo wrote:First you echo, then you set cookie? Won't work.
Set your cookies BEFORE outputting ANYTHING.

Re: Help with: setcookie

Posted: Mon Jan 25, 2010 11:15 am
by Marbled-D
Oooh! Thanks both of you!

Apollo, I didn't realize that setcookie hade to be before any output, what so ever. Sorry... But finally, now I understand.

social_experiment, great! Exactly was I was looking for. Now I guess I could put it as this as well:

Code: Select all

<?php
    if (isset($_COOKIE['huru'])) {
         $PrintOut = $_COOKIE['huru'];
    }
    else {
         setcookie("huru", "Mamma", time()+3600, "/");
    }
   echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
    <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"sv\" lang=\"sv\">";
?>
<body>
<?php
    echo $PrintOut;
?>
</body>
</html>
Is that better, or does ob_start(); and ob_end_flush(); work just as fine?

About:

Code: Select all

<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
No, it's just an HTML (XHTML) page. Copied it from my old webdesign-teacher's site when I understood that you had to have the doctype declaration within php-tags. Should I remove the first line: xml...

Anyhow, thanks again. You saved me. I'm not so good at php yet, but if you want help with flash, maybe I could help.

Re: Help with: setcookie

Posted: Mon Jan 25, 2010 11:37 am
by social_experiment
Yes, remove the xml line.

As for the ob_start() and ob_end_flush(), you must have both included in your document as ob_start() turns on output buffering and ob_end_flush() flushes the buffer and turns off output buffering [SIC].

Re: Help with: setcookie

Posted: Tue Jan 26, 2010 3:41 am
by Apollo
Marbled-D wrote:Is that better, or does ob_start(); and ob_end_flush(); work just as fine?
In my experience, ob_* does not always work flawlessly, this depends on the server. So if you can just as well get it working without, I'd certainly do so to avoid unexpected trouble later on (if you switch servers or whatever).

Re: Help with: setcookie

Posted: Wed Jan 27, 2010 3:44 pm
by Marbled-D
Hey!

Yes, I've changed it all now. Thank's again!