View Full Version : Resolved quick question about COOKIE check
?foru
03-06-2009, 05:51 AM
I've had a cookie based style switcher on my site for a while now and it works great.
switcher.php accessed like switcher.php?set=red
<?php
setcookie ('theirstyle', $set, time()+31536000,
'/', 'mydomain.com', '0');
header("Location: $HTTP_REFERER");
?>
I'm changing things and want to be able to change a snippet in multiple locations in the template rather than tracking down the class and making changes to several .css files
I created the following to try to check if the cookie was set and what it was equal to but I'm missing something because only red.gif is showing if blue or green are set.
<?php
switch ('theirstyle') {
case (isset($_COOKIE['theirstyle']) || $set == 'red'):
echo '<img src="images/red.gif" border="0" alt="alt text here">';
break;
case (isset($_COOKIE['theirstyle']) || $set == 'blue'):
echo '<img src="images/blue.gif" border="0" alt="alt text here">';
break;
case (isset($_COOKIE['theirstyle']) || $set == 'green'):
echo '<img src="images/green.gif" border="0" alt="alt text here">';
break;
default:
//.. anything can go here
break;
}
?>
Does anything stand out to anyone? Thank you
thetestingsite
03-06-2009, 06:01 AM
Instead of using or ( || ) in the if statement, use and ( && ) like so:
<?php
switch ('theirstyle') {
case (isset($_COOKIE['theirstyle']) && $set == 'red'):
echo '<img src="images/red.gif" border="0" alt="alt text here">';
break;
case (isset($_COOKIE['theirstyle']) && $set == 'blue'):
echo '<img src="images/blue.gif" border="0" alt="alt text here">';
break;
case (isset($_COOKIE['theirstyle']) && $set == 'green'):
echo '<img src="images/green.gif" border="0" alt="alt text here">';
break;
default:
//.. anything can go here
break;
}
?>
Hope this helps.
?foru
03-06-2009, 06:52 AM
Thank you for taking a look at this. I changed it to && and didn't get anything. Your explanation helps to think this out though because && is looking for both to be TRUE where || only wanted one.
Not sure what it could be since && didn't work. I know that the cookie is set because I went in and changed the style and the old .css files changed it to the correct images.
JasonDFR
03-06-2009, 07:13 AM
I think this is the best way to do it.
Refresh your browser twice.
<?php
$set = 'blue';
setcookie('theirstyle', $set, time() + 31536000, '/');
$theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
switch ($theirstyle) {
case 'red':
$image = 'RED <img src="images/red.gif" border="0" alt="alt text here">';
break;
case 'blue':
$image = 'BLUE <img src="images/blue.gif" border="0" alt="alt text here">';
break;
case 'green':
$image = 'GREEN <img src="images/green.gif" border="0" alt="alt text here">';
break;
default:
$image = 'DEFAULT <img src="images/green.gif" border="0" alt="alt text here">';
break;
}
echo $image;
?>
JasonDFR
03-06-2009, 09:45 AM
Your example with the switch initially through me off.I just realized that the switch is totally unnecessary. This is best. I think...
In this case the value of the cookie will have to be the name of your image or css file minus the extension.
<?php
$set = 'green';
setcookie('theirstyle', $set, time() + 31536000, '/');
$thierstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
// with some validation of $theirstyle
$image = in_array($thierstyle, array('red', 'blue', 'green')) ?
'<img src="images/' . $thierstyle . '.gif" border="0" alt="alt text here" />' : '<img src="images/red.gif" border="0" alt="alt text here" />';
echo $image;
?>
thetestingsite
03-06-2009, 03:41 PM
Wow, I didn't even realize that was a switch statement. I was so tired when I posted last night that my mind thought those were if statements. I think I really need to start sleeping again at night. :)
?foru
03-07-2009, 04:27 AM
Amazing! Worked like a charm. J both code pieces worked, but I see that the 2nd one is more compact and an interesting way of approaching it (short and precise). I did read in the past that switch statements weren't really needed for 2 to 3 items, but I do however use them in other areas to serve large sections of content by using echo <<< HTML ... HTML;
Since I already had the cookie setup in switcher.php with the redirect I went ahead and kept that, and just removed the set cookie portions above...gaining an "auto" refresh when switcher.php redirects back...haha :D
Did someone say sleep? thetestingsite I hear you, I've been up the last 2 nights until around 3am and getting back up at 7am
Thank you both again for taking a look at this. I appreciate it!
JasonDFR
03-07-2009, 08:56 AM
Cool. Glad it works.
As I continue to get better with PHP I am noticing that my code is becoming more compact and more precise, just like you noticed. So that's great. But when I started, my code was always long and drawn out. I was always thinking, "There must be a better way to do this." It is still a struggle to write compact code, but with practice it does just start to happen.
This forum is a great tool for practicing. Trying to solve "real world" problems has really helped me learn.
Maybe you already are, but if not, start trying to separate your PHP code from the HTML as much as you can. Do all the logic at the top of the page and assign the information you need to variables or arrays, then use them in the body ( display ) of your pages. So in the case of your initial switch statement, don't have it echo anything out, rather assign the result to a varaible, then once it comes time to output the html, use the variable where it is needed.
Good luck!
J
?foru
03-13-2009, 12:24 AM
I've learned quite a bit since joining the forums since there is a great knowledge base, and one nice thing about PHP is that a lot of times there are many ways of doing things. Since I've been working with PHP I've tried to keep it separate from the HTML as much as possible so its easier to work with.
With the code example you provided earlier, I am already starting to use it in ways I've never done before.
$theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
$header_image = in_array($theirstyle, array('red', 'blue', 'green')) ?
'images/header_' . $theirstyle . '.gif' : 'images/header_red.gif';
Since I'm dynamically changing the image I've started to use it in my css as well like so...
#header {background: url(<?php echo $header_image; ?>); background-repeat: no repeat; height: 90px;}
Thank you again.
JasonDFR
03-13-2009, 07:09 AM
I don't think php works in a css file.
The best way to do it, is to put ALL style in CSS files. If you want to have different themes, create other css files, changing only what needs to change for the theme. Then use PHP like you are to change the link to the css file when you output the html.
$theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
$css = in_array($theirstyle, array('red', 'blue', 'green')) ? $theirstyle . '.css' : 'red.css';
Then when you link your css, you could:
<link href="/css/base.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/css/<?php echo $css; ?>" media="screen" rel="stylesheet" type="text/css" />
Good luck!
?foru
03-14-2009, 01:01 AM
You're correct on that and the existing site uses pretty much the same way your presented. I changed the images in the external style just as you mentioned.
<link rel="stylesheet" type="text/css" media="screen" title="User Defined Style" href="./includes/css/<?php echo (!$theirstyle)?'style':$theirstyle ?>.css" />I basically wanted to try something different to shorten the process a little because I currently would have to change all the stylesheets if for example a certain property changed or I wanted to add a new css class...that way all styles related would work.
Sorry for not clarifying. The PHP code inside the css above are only the parts where the images will change and are in the PHP page within <style> tags so it works. The rest of all the css is in the external file to keep it clean. The new way is a little shorter and I learned another clever way of doing the same thing with array() which can be changed to do many other things as well. Thank you.
JasonDFR
03-14-2009, 07:02 AM
You don't have to change all the css files.
If you link your standard css file first:
<link rel="stylesheet" type="text/css" media="screen" title="Standard" href="./includes/css/standard.css" />
<!-- then the user defined style -->
<link rel="stylesheet" type="text/css" media="screen" title="User Defined Style" href="./includes/css/<?php echo (!$theirstyle)?'style':$theirstyle ?>.css" />
The settings in the user defined will override the settings in the standard.
So if your standard css has:
/* Header
-------------------------------------------------------------- */
div#header {
margin: 0;
overflow: hidden;
}
div#header h1 {
float: left;
margin: 0;
text-indent: -9999px;
}
div#header h1 a {
display: block;
width: 247px;
height: 56px;
background: transparent url('/images/header_logo.gif') left top no-repeat;
border: none;
}
And you only want to change the header_logo in a user defined, your user defined css file only needs one line:
div#header h1 a { background: transparent url('/images/different_logo.gif') left top no-repeat; }
Since the user defined css file is linked after the standard one in your HTML markup, all the standard css will apply, then the the user defined css will override the standard, in the above case only changing the div#header h1 a element style, and leaving everything else the same.
This allows you to change the width of a div for example, that will affect the whole site in one place, just your standard.css. You don't have to touch the user defined.
If you have any questions, let me know. Because based on what you said: "I basically wanted to try something different to shorten the process a little because I currently would have to change all the stylesheets if for example a certain property changed or I wanted to add a new css class...that way all styles related would work." Makes it sound like you are duplicating your whole style sheet for each of the user defined ones, rather than just changing the elements relative to each user style and not linking more than one style sheet to your HTML at any one given time.
?foru
03-14-2009, 07:38 PM
I believe back when I originally created the multiple styles quite some time ago I over coded as you suggested because the stylesheets were the same and the images were changed just in those spots. What you mentioned makes perfect since looking back on it. I could have made the changes in the "global" layout /css/global.css which would control most of the site(already included in site) and the minor changes could have been made to the one line on the user defined style.
I did however use this effective little snippet back then to add some IE styles if IE was the detected browser.
<?php if ( ! (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') === false) ) echo '<link rel="stylesheet" type="text/css" href="./includes/css/ie.css">'; ?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.