Cookie problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Cookie problem...

Post by Aaron »

Ok so Instead of having multiple php pages I decided Ill use variables thatll set the cookie to set the theme.


Starts simply with

Code: Select all

if(!$_COOKIE['theme'])
{if($theme == "wuggy")  
{setcookie ("theme", "wuggy", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}  
else
{setcookie ("theme", "default", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}}


Basically, if the user doesnt define theme in the url then it should automatically set the $_COOKIE['theme'] to default...however it does and doesnt sometime...ANYWAY

onto the actual CSS switch....

Code: Select all

<link rel="stylesheet" href="includes/styles/<? echo "".$_COOKIEї'theme']."" ?>.css" type="text/css">


well, it just isnt working no matter what I do...you can check it out forumz.wuggawoo.co.uk (dont use the scroll box at the top because that just changes the page (index.php to wuggy.php) - only index.php has the above code in.

To test just bung ?theme=wuggy or ?theme=forumz and you should note that it doesnt work :(
leoden
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 8:48 pm

Post by leoden »

Is the cookie set anywhere else, as you have a 'Not' operator in your opening if statement, It may be an idea to debug inside this if statement to check that your actually gettting inside it

Code: Select all

if(!$_COOKIEї'theme'])
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

The theme string of the cookie isnt set anywhere else.

Theres no need for an else because the theme only needs to be set once. Ill debug by making the else set outlaw = 1, and see what outlaw equals.
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

Testing.... I did this....

Code: Select all

if(!$_COOKIE['theme'])
	{$first= 1;
	if($theme == "wuggy")  
	{$second = 2;
	setcookie ("theme", "default", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}  
	else
	{$third = 3;
	setcookie ("theme", "forumz", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}}
	else
	{$forth = 4;}
going straight to the forum I got; first = & second = & third = & forth = 4

using ?theme=forumz I get first = & second = & third = & forth = 4
using ?theme=wuggy I get first = & second = & third = & forth = 4

So the cookies set, so I figured I'd modify the code to...

Code: Select all

if(!$_COOKIE['theme'] || $theme)
	{$first= 1;
	if($theme == "wuggy")  
	{$second = 2;
	setcookie ("theme", "default", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}  
	elseif($theme == "forumz")
	{$third = 3;
	setcookie ("theme", "forumz", time()+60*60*24*30,'/','.wuggawoo.co.uk', 0);}}
	else
	{$forth = 4;}
and now get first = 1 & second = & third = & forth =

no matter what....even If I dont include theme=whatever in the url!?

http://forumz.wuggawoo.co.uk/?section=index test for yourself.
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

First off, I did put a ?> in the CSS part, but the code and PHP tag dont show it up O_o Nick helped me out and got it working great...

Code: Select all

if($submitted == true) 
{setcookie("theme", "theme:$tc", time()+604800, "/"); 
header("Location: $HTTP_REFERER");} 
 
if(!$_COOKIE['theme']) 
{setcookie("theme", "theme:default", time()+604800, "/"); 
header("Location: index.php");} 
else 
{global $theme; 
$session_vars = explode(":", $theme);}


Then where the CSS bit is we use...

<link rel="stylesheet" href="includes/styles/<? echo $session_vars[1]; ?>.css" type="text/css">

and the drop down box is simply just

Code: Select all

<form name="form1" method="post" action="index.php"  style='margin-top : 0px;margin-bottom : 0px;'> Wuggawoo forum 
<select name="tc" class='textfield' onChange="document.form1.submit();"> 
<option value="default">-Select-</option> 
<option value="default">Forumz</option> 
<option value="wuggawoo">Wuggawoo</option> 
</select> 
<input name="submitted" type="hidden" value="true"> 
</form>


thx anyway!
Post Reply