...

View Full Version : Setting cookies and refreshing the page



]|V|[agnus
09-23-2004, 01:30 AM
Due to PHPs annoying cookies-aren't-recognized-until-next-page-load feature, I want to refresh the page as soon as the cookie is set so that users will see the changes they just made rather than go, "Huh?" Crazy, I know. (Perhaps somebody could explain why PHP thinks its a good idea to handle cookies this way.)

I've got the PHP Cookbook from O'Reilly which covers how to set cookies(not really having any problems there) and also how to redirect a page using the Location header, but for some reason it is not working for me.

Here's what I got:



<?
if (isset($_POST["submitted"])) {
// ingredients
$e = time() + (365 * 86400); // expiration (1 year)
$d = "sethrasmussen.com"; // domain
switch($_POST["submitted"]) {
case "layout" :
switch ($_POST["layout"]) {
case "fixed" :
setcookie("layout","fixed",$e,'',$d);
break;
case "fluid" :
setcookie("layout","fluid",$e,'',$d);
break;
default :
setcookie("layout","fluid",$e,'',$d);
}
break;
default :
}
// refresh
header("Location: http://sethrasmussen.com/prefs.php");
}
?>


Is there a better way to do a page refresh, or am I missing something obvious?

Nightfire
09-23-2004, 01:38 AM
It has to send the data to the server for the cookie to set, that's what a serverside language does. If you want a cookie created on the fly, then use javascript.

I don't think you can have 2 switches embedded like that either. Could be wrong though, but I've never seen them like that before

]|V|[agnus
09-23-2004, 01:51 AM
Why on earth couldn't you nest a switch like that? It's just a condensed way of writing a complex if statement.

As for the rest, I understand how SSLs work, thank you very much, and I am aware of how PHP handle's cookies obviously. It is annoying to me because ColdFusion has access to the cookies it sets immediately, a smart behavior. I don't know if that is because it sets client cookies by default or what.

Regardless, I want to know how to refresh the page and/or why my header() function is not doing the job. Thanks for finding the time to address that amidst your many other helpful comments.

firepages
09-23-2004, 02:49 AM
Perhaps somebody could explain why PHP thinks its a good idea to handle cookies this way

dunno , being as PHP is a server-side language in a stateless protocol your suggestion is ?

anyway even though the cookie itself can not be read on the same page , we assume that you know at this point what the contents are , so set them.. (just remember that at this point you do not know if the cookie has been accepted or not & whether it will be available on the next page



<?
if($condition){
setcookie("layout","fixed",$e,'',$d);
$_COOKIE['layout'] = 'fixed';
}
?>



As for your problem well its a weird one , but in cases of such weirdness (often after seting headers of some description) I often try sticking <?exit();?> right after the header call.

firepages
09-23-2004, 02:53 AM
aghh ]|V|[agnus , you really are going to have to curb your sarcasm in this forum , Nightfire was trying to help , take it in good faith or keep it to yourself.

]|V|[agnus
09-23-2004, 03:08 AM
I guess I got confused about how not addressing the topic and nitpicking details that *might* be relevant("I don't think you can do that, but I'm not sure...") are helpful. A little sarcasm is no big deal when it has a valid point. That is, if you can take it in stride... :cool: But yeah, I get ya...



dunno , being as PHP is a server-side language in a stateless protocol your suggestion is ?

Automatically be aware of cookies set before the end of a script? I dunno, whatever ColdFusion does. Again, it's a smarter behavior, unless somebody can bring to light how I'm wrong.

Your suggestion of adding that additional assignment works to achieve the effect I want without requiring a refresh. That seems like an annoying extra step, but perhaps I take it for granted simply because ColdFusion automatically does that extra step for you no matter what, who knows?

firepages
09-23-2004, 07:32 AM
...Automatically be aware of cookies set before the end of a script?..

perhaps thats how CF works ? , if this old tutorial is correct (and I don't know) then coldfusion simply does what we have done above ..


the <cfcookie> code has to store the variable at a local level and process the page as if the cookie is already set, even though it doesn't store the variable until the HTTP headers are returned.
http://webmonkey.wired.com/webmonkey/00/29/index3a_page4.html?tw=programming


However thats bad juju in that you don't know for sure that your cookie is set until you try to access it from the client later on, cos it might not be there , now if you are aware of this from the beginning (as we are in the PHP case) we can account & compensate for possible issues arising from the case of the missing cookie.

Pehaps CF rewrites tags with a session id in this case (as with PHP sessions)

PHP sessions can fill in as session variables are ready to use immediately regardless of cookie acceptance ,though more restrictive PHP installations may insist on cookie propagation for sessions , breaking sessions for non-cookie enabled users.

Out of interest did the exit(); not work ?

raf
09-23-2004, 09:04 AM
|V|[agnus']Why on earth couldn't you nest a switch like that? It's just a condensed way of writing a complex if statement.
:confused: What would be complex about


if ($_POST['submitted'] == 'layout') {
switch ($_POST["layout"]) {
case "fixed" :
setcookie("layout","fixed",$e,'',$d);
break;
case "fluid" :
setcookie("layout","fluid",$e,'',$d);
break;
default :
setcookie("layout","fluid",$e,'',$d);
}
}

about the cookies, well, it's obvious why you can't access the cookies on the page that sets than, because they are sent to the server with a pagerequest in the header.
there isn't any point in being able to access them on the page that sets them. All info you add to the cookie is know anyway (because you're sending it) and all info that was already inside an existing cookie is also known.
there is no way that any server side language could know if cookies are set inside the page that sets them.

this only creates problems if you wanna check if the user acepts cookies on the very first page of your site.

without knowing what you try to do, i'm quite sure that sessions would be a better approach. Cookies are only usefull to store data that needs to be persistent after the session ends, and that isn't important enough to store it serversded (or that you can't link to a user or machine because there's no login or whatever.)

]|V|[agnus
09-23-2004, 02:16 PM
:confused: What would be complex about

Nothing. Again, this page will eventually have multiple forms against which I will be checking their "submitted" value to identify which one is submitting. So the switch will help condense that code. I was also simply using that phrase to express the purpose of a switch in general.


without knowing what you try to do, i'm quite sure that sessions would be a better approach.

You could very well be right. I've not gotten that far in converting my ColdFusion skills yet. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum