Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting cookies and refreshing the page

    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:

    PHP Code:
    <?
    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?

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    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

    PHP Code:
    <?
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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... But yeah, I get ya...

    Quote Originally Posted by firepages
    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?

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    ...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...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 ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ]|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.
    What would be complex about
    PHP Code:
    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.)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    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.

    Quote Originally Posted by raf
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •