Results 1 to 11 of 11
Thread: PHP: Cookies
-
07-11-2004, 01:16 PM #1Web Hosting Evangelist
- 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", $value, time()+60, ".gtaisland.gtagaming.com");
?>
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');
}
Any help is appriciated. Thanks.
-
07-11-2004, 02:54 PM #2Web Hosting Master
- 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", $value, time()+60, ".gtaisland.gtagaming.com");
?>
-
07-11-2004, 03:59 PM #3Web Hosting Evangelist
- 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>";
?>
-
07-13-2004, 10:30 AM #4Web Hosting Evangelist
- 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.
-
07-13-2004, 11:41 AM #5Web Hosting Guru
- 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);}
-
07-13-2004, 12:16 PM #6Web Hosting Evangelist
- 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'].
-
07-13-2004, 12:22 PM #7Web Hosting Master
- 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\">";
[/edit]Last edited by Newuser11123; 07-13-2004 at 12:32 PM.
-
07-13-2004, 12:53 PM #8Web Hosting Evangelist
- Join Date
- May 2004
- Posts
- 500
No luck with that idea either It doesn't even set the cookie now.
-
07-13-2004, 12:58 PM #9Web Hosting Master
- 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.
-
07-13-2004, 06:13 PM #10Junior Guru Wannabe
- Join Date
- Dec 2003
- Posts
- 97
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", $value, time()+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');
}
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>";
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]]]]])
-
07-14-2004, 10:49 AM #11Web Hosting Evangelist
- 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::.