...

View Full Version : don't get cookies



ASAAKI
05-23-2003, 07:36 PM
i've searched around as well as checked on php.net... i still don't think i've got it with cookies.
i've got a page '1.php', that has a form with a userId textbox. Users insert their id in 1.php, and submit the form. then 2.php comes up and they can see their id. when they go back to 1.php and refresh it, their id's still there. that's fine. it means the cookie did kind of work.
but if they go around somewhere else or close the window or something, and then come back, the cookied id's gone again.
:confused:

here's my code for 1.php:


<?php
if($_COOKIE['id']){
$cookieId=$_COOKIE['id'];
}
?>
.
.//do other stuff...
.
<?php
echo("<input type='text' class='txt' name='userid' value='{$cookieId}' onfocus=\"this.value=='ID' ? this.value=='' : null;\" />");
?>


and on 2.php is where the cookie is actually set:


<?php

if($_GET){
setcookie("id",$_GET['userid'],time()+60*60*24*30);
}
echo("Welcome, {$_COOKIE['id']}!");

?>

Jason
05-23-2003, 08:24 PM
I don't know much about cookies myself but it sounds like the cookie is valid only on the current visit which would indicate that the time varriable for the cookie isn't working right. Might want to read up about how that part works.


Jason

duniyadnd
05-23-2003, 08:45 PM
setcookie("id",$_GET['userid'],time()+60*60*24*30, "/" , "./yourdomainname", 0);

Try that instead. Don't forget that you'll need the period (.) before the slash in front of your domainname.

The attribute after the time basically said the cookie is valid all over the site, not just a particular folder.

Duniyadnd

ASAAKI
05-23-2003, 09:13 PM
actually i just tested my old code again. what happens with that is that on one refresh, the cookie works, but on the second refresh it's gone. and on moving around, it's definitely gone.

now with adding ,"/","./http://127.0.0.1/",0 in the setcookie function, it doesn't remember the id at all, after any refreshes...:confused:

am I calling the cookie wrongly on 1.php or something?

Jason
05-23-2003, 09:24 PM
no, the desitnation is wrong...that cookie you put on someones computer is using the "whole site" set at "127.0.0.1" which is also localhost so the cookie would apply to the users computer instead of your server. Try and change the IP to match the IP or URL of your site...


Jason

ASAAKI
05-24-2003, 12:46 AM
oops sorry, maybe i should have clarified...this is not on any 'real' site, i've got apache on my machine and i'm testing cookies on my own localhost :D

ASAAKI
05-25-2003, 07:07 AM
okay it's like this.
shoutbox.php is the file where the cookied id should appear.
shouts.php is a file in an iframe within shoutbox.php

shoutbox.php:


<?php
if($_COOKIE['id']){
$cookieId=$_COOKIE['id'];
echo($cookieId."<BR>");
var_dump($_COOKIE);
}else{$cookieId="ID";}
?>
.
.
.
<?php
echo("<input type='text' class='txt' name='shouter' value='{$cookieId}' onfocus=\"this.value=='ID' ? this.value=='' : null;\" />");
?>
.
.
.


and shouts.php:


...

if($_GET){
$newshout=$_GET['shouttext'];
$newname=$_GET['shouter'];
setcookie("id",$_GET['shouter'],time()+60*60*24*30, "/" , "./asaaki.ithium.net", 0);

...


i kinda had the idea cookies were really easy...:(

Nightfire
05-26-2003, 04:01 AM
I have no idea what's wrong, I've had cookies working on my localhost with the very first snippet you showed. Maybe it's something to do with your cookie settings on your browser? :confused: and by the looks of the slowness of replies, noone else can see what's wrong



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum