php cookie help [solved] - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: php cookie help [solved] (/showthread.php?tid=39741)
php cookie help [solved] by Ezra on 03-07-2005 at 10:13 PM
I'm learning php now, and i'm trying to set cookie's, but it won't work
code: <form action="cookie.php" method="post">
<input type="text" name="naam" value="<?php $_COOKIE["naam"] ?>">
<input type="submit" value="Cookie Instellen" name="Submit">
<input type="button" onClick="<?php setcookie ("Naam","",time()-3600)?>" value="Cookie Wissen">
</form>
<?php
//cookies instellen
setcookie ("Naam", $_POST["naam"],time()+86400); /* onthoud naam voor 24 uur*/
setcookie ("Ipadres", $_SERVER["REMOTE_ADDR"], time()+86400); /*onthoud ip voor 24 uur */
//dingen naar de pagina printen
print "Als je binnen 24 uur op deze pagina terugkomt, weet het script je naam nog en je ipadres";
print "<br>" . "Naam: " . $_COOKIE["naam"] . "<br>" . "Ip-adres: " . $_COOKIE["Ipadres"];
?>
Does anyone know what i'm doing wrong?
RE: php cookie help by L. Coyote on 03-07-2005 at 10:24 PM
quote: Originally posted by Ezra
value="<?php $_COOKIE["naam"] ?>">
You are missing an "echo"
code: value="<?php echo $_COOKIE["naam"] ?>">
Edit: I now see the problem, I didn't look at it closely before.
You can't use JavaScript events with PHP. "onClick" is JavaScript/VBScript ONLY.
You can set the cookie via JavaScript, though. Also the page needs to be sent to the server in order for PHP to get the cookie.
RE: php cookie help by Ezra on 03-07-2005 at 10:28 PM
Ur right , but that won't fix it , cause i'm printing the cookies at the end too...
EDIT: I didn't know i couldn't mix javascript and php the way I did, but that button was just to delete the cookie. And now I removed the whole button it still doesn't work
RE: php cookie help by L. Coyote on 03-07-2005 at 10:36 PM
Cookie names are case-sensitive. You're calling naam, but you're setting Naam. Maybe correcting that will make it work.
Edit: I'm stupid today.
RE: php cookie help by Ezra on 03-07-2005 at 10:41 PM
I can see that my code was full of stupid bugs , and i corrected all of them... BUT, it's still not working
My updated code
code: <form action="cookie.php" method="post">
<input type="text" name="naam" value="<?php echo $_COOKIE["naam"]?>">
<input type="submit" value="Cookie Instellen" name="Submit">
</form>
<?php
//cookies instellen
setcookie ("Naam", $_POST["naam"], time()+86400, "/", "", 0); /* onthoud naam voor 24 uur*/
setcookie ("Ipadres", $_SERVER["REMOTE_ADDR"], time()+86400, "/", "", 0); /*onthoud ip voor 24 uur */
//dingen naar de pagina printen
print "Als je binnen 24 uur op deze pagina terugkomt, weet het script je naam nog en je ipadres";
print "<br>" . "Naam: " . $_COOKIE["Naam"] . "<br>" . "Ip-adres: " . $_COOKIE["Ipadres"];
?>
RE: php cookie help by Plik on 03-07-2005 at 10:45 PM
Does it give any errors? Or just plain not work
RE: php cookie help by L. Coyote on 03-07-2005 at 10:46 PM
You're hitting submit or refreshing the page?
I too have a problem for not seeing obvious mistakes, so maybe that's why I can't point it out.
RE: php cookie help by Ezra on 03-07-2005 at 10:50 PM
try the updated code for urselve http://school.tsdme.nl/cookie.php
And yes I am hitting Submit , and after that i even tried submitting and then refreshing and after that even opening the page fresh-fresh
@Madman, the page loads ok, but it won't set the cookies
RE: php cookie help by Plik on 03-07-2005 at 10:55 PM
code: setcookie ("Naam", $_POST["naam"], time()+86400, "/", "", 0);
Thats ment to be a boolean not a integer
so
code: setcookie ("Naam", $_POST["naam"], time()+86400, "/", "", False or True);
would work (Note: chose between true or false)
Also you dont have to set all the parameters, unless you specifically need to controll the path and securty etc.. you could use
code: setcookie ("Naam", $_POST["naam"], time()+86400);
RE: php cookie help by L. Coyote on 03-07-2005 at 10:57 PM
quote: Originally posted by Ezra
<?php echo $_COOKIE["naam"]?>
Should be Naam.
RE: php cookie help by Ezra on 03-07-2005 at 11:00 PM
@ Madman thnx for clearing that up. And i changed it to
code: setcookie ("Naam", $_POST["naam"], time()+86400); /* onthoud naam voor 24 uur*/
setcookie ("Ipadres", $_SERVER["REMOTE_ADDR"], time()+86400); /*onthoud ip voor 24 uur */
And... still nothing
RE: php cookie help by Plik on 03-07-2005 at 11:12 PM
You cant set cookies after the page has outputed anything.
And some times the page will be called when there isnt anything posted, or no cookies, so you have to check to see if there set
So use this
code: <?php
//cookies instellen
If($_POST){ //check to see if anything has been posted to the page
setcookie ("Naam", $_POST["naam"], time()+86400); /* onthoud naam voor 24 uur*/
setcookie ("Ipadres", $_SERVER["REMOTE_ADDR"], time()+86400); /*onthoud ip voor 24 uur */
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Cookie Test</title>
</head>
<body>
<form action="cookietest.php" method="post">
<input type="text" name="naam" value="<?php IF(isset($_COOKIE["Naam"])) echo $_COOKIE["Naam"] ?>">
<input type="submit" value="Cookie Instellen" name="Submit">
</form>
<?php
if(isset($_COOKIE["Naam"]) AND isset($_COOKIE["Ipadres"])){ //Check to see if the cookies are set
//dingen naar de pagina printen
print "Als je binnen 24 uur op deze pagina terugkomt, weet het script je naam nog en je ipadres";
print "<br>" . "Naam: " . $_COOKIE["Naam"] . "<br>" . "Ip-adres: " . $_COOKIE["Ipadres"];
}
?>
</body>
</html>
hope it makes sense
700th post
RE: php cookie help by L. Coyote on 03-07-2005 at 11:16 PM
I knew I'd find out after someone else pointed it out.
I suddenly feel old...
RE: php cookie help by Ezra on 03-07-2005 at 11:23 PM
It works
I should read www.php.net better next time i try something
|