Results 1 to 11 of 11

Thread: PHP: Cookies

  1. #1
    Join Date
    May 2004
    Posts
    500

    PHP: Cookies

    I have been trying to get my head around this for a while now...well the best part of today anyway.

    I am making a system so that visitors can change themes. So what I need to do is, if the cookie says Red then it uses the red style sheet, if the cookie says Blue then it uses the Blue style sheet. Pretty straight forward. So far I have created a form that will set the cookie...like so;

    PHP Code:
    <?php
    setcookie
    ("theme"$valuetime()+60".gtaisland.gtagaming.com");
    ?>
    The expire time is low just because I am testing at the moment.

    So in the header where I include the style sheet that matches the cookie I have the following code;

    PHP Code:
    if($_COOKIE['theme'] == 'Red'){
        include(
    'http://gtaisland.gtagaming.com/includes/red.css');
    }else{
        include(
    'http://gtaisland.gtagaming.com/includes/blue.css');

    That is just under the head html tag. Where it should be? Why doesn't this work. For some reason it doesn't matter what the cookie says, it always includes the blue style sheet. I have opened up the cookie to make sure it was being set...and as far as I can see it is. If I want it to be set as blue then it says blue in the cookie, likewise it says red if I set it as red.

    Any help is appriciated. Thanks.

  2. #2
    Join Date
    Feb 2003
    Posts
    717
    havnt had a chance to reade thorugh your code, but could you post the form here? that might help us and where is the
    PHP Code:
    <?php 
    setcookie
    ("theme"$valuetime()+60".gtaisland.gtagaming.com"); 
    ?>
    being set? (where in the file, like top, very top, under something, etc.)

  3. #3
    Join Date
    May 2004
    Posts
    500
    Sorry, here's the form;
    PHP Code:
    <?php
    echo "<form action=\"setcookie.php\">";
    echo 
    "<select multiple size=\"4\" name=\"value\">";
    echo 
    "<option>Blue</option>";
    echo 
    "<option>Red</option>";
    echo 
    "<input type=\"submit\" value=\"Submit\">";
    echo 
    "</form>";
    ?>
    At the moment the set cookie thing is on its own, just while I test it out and stuff. Then when I get it working I will put it in my site somewhere.

  4. #4
    Join Date
    May 2004
    Posts
    500
    Though I should save this unanswered thread from the dark depths of the second page.

    Any ideas on why this isn't working? Or any suggestions for another way to do it?

    I was thinking about sessions but then the person would have to be a member. I just want every visitor to be able to change the theme.

  5. #5
    Join Date
    May 2004
    Location
    Singapore
    Posts
    263
    Have you considered using $_POST['value'] instead of $value?

    Have you tested to ensure that the cookie is set (i.e. on the next page load)?
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  6. #6
    Join Date
    May 2004
    Posts
    500
    The cookie has definatly set because when I open the cookie it contains either "Red" or "Blue" depending on what I chose on the form. Well, it did until I changed the $value to $_POST['value'].

  7. #7
    Join Date
    Feb 2003
    Posts
    717
    instead of $_POST['value'], try $_GET['value']; as you are using the GET method in your form, so $_POST would not pick it up.
    [edit]
    replace:
    echo "<form action=\"setcookie.php\">";
    with:
    Code:
    echo "<form action=\"setcookie.php\" method=\"get\">";
    just to be safe so you can call $value with $_GET
    [/edit]
    Last edited by Newuser11123; 07-13-2004 at 12:32 PM.

  8. #8
    Join Date
    May 2004
    Posts
    500
    No luck with that idea either It doesn't even set the cookie now.

  9. #9
    Join Date
    Feb 2003
    Posts
    717
    try having a look at: http://www.webhostingtalk.com/showth...hreadid=294310
    that might be the cause to your old problem, and make sure that the method in the form is the same as the request method for the cookie script.

  10. #10
    First off, program like Register Globals is off, and you are writing it in C, where an uninitilized variable may have garbage.

    PHP Code:
        $value="";
        if (isset(
    $_POST["value"])) $value=$_POST["value"];
        
    setcookie("theme"$valuetime()+60"/"".gtaisland.gtagaming.com");

        if (isset(
    $_COOKIE["theme"])) $value=$_COOKIE["theme"];

        if(
    $value="Red"){
            include(
    'http://gtaisland.gtagaming.com/includes/red.css');
        }else{
            include(
    'http://gtaisland.gtagaming.com/includes/blue.css');
        } 
    Also the form:

    PHP Code:
    echo "<form action=\"setcookie.php\" method=\"POST\">";
    echo 
    "<select multiple size=\"4\" name=\"value\">";
    echo 
    "<option value=\"Blue\">Blue</option>";
    echo 
    "<option value=\"Red\">Red</option>";
    echo 
    "<input type=\"submit\" value=\"Submit\">";
    echo 
    "</form>"
    That should work on submit. If the cookie value part does not work, try modifying the domain part.

    Your only problem could of been solved by looking at the PHP documentation for setcookie:

    Description
    bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
    So you left out the path part

  11. #11
    Join Date
    May 2004
    Posts
    500
    Thanks for all your help. I managed to get it working. I'm not sure if I'll use it, but thanks any way. I have decided its a bit pointless but its good to know so I can expand on it some other time.

    Thanks

    .::Mouldy Punk::.

Posting Permissions

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