Forum Moderators: coopster

Message Too Old, No Replies

Problem with cookie and displaying content

         

Cher

6:54 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



I have a question about setting cookies.

I am utilizing a css style switcher developed by another page but I have had to change some things to get it to work with my iframe. I wanted 1 style to use the iframe and then a text-based style that loads outside the iframe. I had everything working fine until I went to view the page on another pc.

The styler sets a cookie with the user's prefered style sheet and then displays the page. I figured out that I could read the cookie from the index.php file (iframe mode) and if the cookie was set with a specific value the page would redirect to the start of the text-based page. It works just fine as long as the cookie is set.

On the other pc I tried it on the cookie is *not* set yet and I've come across a problem. The page doesn't display at all! I've tried adding the setcookie() at the top of the cookie-checker but that has no affect.


<?php
if(isset($_COOKIE['mycookie'])){
$style = trim($_COOKIE['mycookie']);
if($style == "graphic_no"){
header("Location: http://my.web.site/textver.php");
exit;
}
else{
?>
display graphic ver html page
<?php
}
}
?>

I was thinking if there wasn't a cookie set at all then it would default to the graphic version, or if the user had the grahic version css selected that is stored in the cookie. Where am I going wrong?

Cher

10:33 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



Seems as though I fixed it on my own


<?php
if(isset($_COOKIE['mycookie'])){
$style = trim($_COOKIE['mycookie']);
if($style == "graphic_no"){
header("Location: http://my.web.site/textver.php");
exit;
}
}
if(!isset($_COOKIE['mycookie']) ¦¦ ($style == "graphic_yes")){
include("graphicver.php");
}

?>

stecostello

10:35 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



I think the problem is in your coding, you forgot to close the origional if statement, stating that if the cookie exists to check what the cookie value equals, try replacing your code with this :

<?php
if(isset($_COOKIE['mycookie'])){
$style = trim($_COOKIE['mycookie']);
if($style == "graphic_no"){
header("Location: [my.web.site...]
exit;
}}elseif(!isset($_COOKIE['mycookie'])){
?>
display graphic ver html page
<?php
}
?>