Closed Thread Icon

Preserved Topic: Cookie sucks! Pages that link to <a href="https://ozoneasylum.com/backlink?for=20992" title="Pages that link to Preserved Topic: Cookie sucks!" rel="nofollow" >Preserved Topic: Cookie sucks!\

 
Author Thread
lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-25-2001 11:37

Hi!
I've been trying to make my way with a simple PHP & Cookies example but i failed!

Can someone plz write me an example of setting a cookie and updating it's values?
like a form to select a value from a [select] and then the user submits, and upon submition the title changes to "Welcome $username", if he reselects and submits again the title changes again...

Thanks!

timothymcnulty
Neurotic (0) Inmate
Newly admitted
posted posted 06-25-2001 13:58

Soemthing like this should work...

cookies.php

code:
<?


if ($UsrCookie) {
echo("Welcome $UsrCookie, change your user name below");
}else{
echo("You do not have a User Name (or are not accepting cookies) enter a user name below:");
}

?>

<form action="sendcookie.php">
<input type="text" name="usrName" size="20" maxlength="20">
<input type="submit" name="Submit" value="Submit">
</form>



sendcookies.php

code:
<?

setcookie("UsrCookie",$usrName,time()+86400);

header("Location: cookies.php");

?>



what kind of problems are you having? maybe we could clarify why you are having those problems...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-26-2001 01:19

I actually had some problems using cookies with PHP as well, I never really pinpointed the problem and ended up using a Javascript solution instead.

sorry I guess this doens't really help but I feel your pain


Walking the Earth like Kane

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 06-26-2001 07:56

Cookies are evil. But, I never had any problems with them, he he heee...

timothymcnulty
Neurotic (0) Inmate
Newly admitted
posted posted 06-26-2001 08:36

That's funny Max I was going to say just about the same thing...

Cookies are so damn easy, especially in PHP...there is only one function: setcookie(). It is used to create and delete cookies. Also, any cookies you set are loaded as global variables with no effort on your part. I don't really like using them, but I don't think that they are too complex...


edit...gunny to funny, what the hell is gunny?

[This message has been edited by timothymcnulty (edited 06-26-2001).]

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-26-2001 15:00

well...i wanted an example with same page setting/updating values of a cookie easily!
all in one page!

anyway as bitdamanged...i'm using JavaScript to set/update cookie after i failed doing so with PHP...
now cookies become so easy with JS



timothymcnulty
Neurotic (0) Inmate
Newly admitted
posted posted 06-26-2001 16:06

Nowhere did you mention that you wanted it on the same page...if you were more specific I could have told you it was not possible...

RTFM...You cannot read a new cookie value on the same page that it is set...you will be reading the old value, if there is one...

Excerpt from PHP manual:
"Cookies will not become visible until the next loading of a page that the cookie should be visible for."

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 06-26-2001 20:45

Actually, you can set & read cookies on the same page, but you have to reload page after setting them. Here's one example from PHP's web site:

<?php
$status = 0;
if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
if (!isset($CCHK)) {
setcookie("myTstCky", "ChocChip");
header("Location: $PHP_SELF?CCHK=1");
exit;
}
?>
<html>
<head><title>Cookie Check</title></head>
<body bgcolor="#FFFFFF" text="#000000">
Cookie Check Status:
<?php
printf ('<font color="#%s">%s</font>
;',
$status ? "00FF00" : "FF0000",
$status ? "PASSED!" : "FAILED!");
?>
</body>
</html>

BTW Instead of setting Location in HTTP header, I would prefer Refresh command in HTTP header (it works much better when you also use PHP4's sessions).

timothymcnulty
Neurotic (0) Inmate
Newly admitted
posted posted 06-27-2001 08:10

That is my point, don't expect to set a cookie and read its newly set value on the same page...the new value of the cookie will only be available when the page is loaded again (it seems obvious to force the page to refresh)...my mistake I should have added that in as well...thanks for expanding max...

« BackwardsOnwards »

Show Forum Drop Down Menu