Page 1 of 1

What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 2:25 am
by crazylegsmurphy
I am starting a new topic because I think my old one is way too jumbled. I think it's best if I start with a clean slate so you can all follow me.

I have 3 files.

top.php
index.php
bottom.php

The index.php (content) uses includes to build the pages (top/bottom).

Now, in top.php I have the following code:

Code:

Code: Select all

 
<?php
session_start();
$_SESSION["css"] = ($_GET['css']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
  <title>carraigeridge.com</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="keywords" lang="en" content="" />
  <meta name="description" lang="en" content="" />
  <meta name="copyright" content="" />
  <meta name="robots" content="all" />
  
    <script type="text/javascript" src="/_scripts/lightbox/prototype.js"></script>
    <script type="text/javascript" src="/_scripts/lightbox/scriptaculous.js?load=effects,builder"></script>
    <script type="text/javascript" src="/_scripts/lightbox/lightbox.js"></script>
    <script type="text/javascript" src="/_scripts/external.js"></script> 
  
    <link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/lightbox.css"  />
    <link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="/_stylesheets/print.css" />
    <!--[if IE 6]>
    <link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="/_stylesheets/print-ie.css" />
    <![endif]-->
 
 
 
 
<?php 
$date = date("n");
if (!isset($_GET['css'])) {
if ($date >= 4 && $date <= 5 || $css  == 'spring'){ ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } elseif ($date >= 6 && $date <= 8 || $css  == 'summer') { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= 9 && $date <= 10 || $css  == 'autumn') { ?> 
<link rel="stylesheet" href="/_stylesheets/autumn.css" type="text/css" media="screen" />
<? } elseif ($date >= 11 && $date <= 3 || $css == 'winter') { ?>
<link rel="stylesheet" href="/_stylesheets/winter.css" type="text/css" media="screen" />
<? } else { ?>
<link rel="stylesheet" href="/_stylesheets/screen.css" type="text/css" media="screen" />
<? }; 
} else { ?>
<link rel="stylesheet" href="/_stylesheets/<?php echo $_GET['css']; ?>.css" type="text/css" media="screen" />
<? }; ?>
 
    
    
</head>
<body>
 
<div id="wrapper">
 
    <div id="wrapper-content">
 
        <div id="header">
        <div class="header-h"></div>
        
            <ul class="seasons"> 
              <li><a href="?css=spring" title="Spring">Spring</a></li>
              <li><a href="?css=summer" title="Summer">Summer</a></li>
              <li><a href="?css=autumn" title="Autumn">Autumn</a></li>
              <li><a href="?css=winter" title="Winter">Winter</a></li>
            </ul>

In the index file I have the following code:

Code:

Code: Select all

 
<?PHP
session_start();
print $_SESSION['css']; 
 
$section='community';
include('../_includes/top.php');
?>
 
<div id="content">
 
    <div id="content-wrapper">
    
        <div id="content-info"> 
        
            <div id="content-title"><h1>Engage, Inspire...</h1></div>
            
            <div id="content-image-community"><span>Community Image</span></div>   
        
        </div>
        
        <div id="content-links"><?php include("links.php"); ?></div>
        
        <div id="content-links-external">
        
        <ul class="content-links">
            <li></li>
        </ul>
        
        </div>
        
    </div>
 
</div>
 
<?PHP
include('../_includes/bottom.php');
?>



and the bottom.php simply finishes the page.


If you take a look at the top.php file you'll see that I have some PHP that loads a CSS file based on either the date, or the variable set by the user.

Below that in the same file you see some links that send variables such as "?css=spring".

The goal, is for the site to load the appropriate css file based on the season, but override that if the user clicks one of the season links and stay that way until the user closes the page.

What is happening is that the date is working, and the links are working, but what happens is when I am on a page, and I click "Autumn" for example, it will change the css file to autumn for that page, but if I then go to another page of the site, the css file defaults back to the date.

At first I thought the variable was getting lost, but it turns out after adding the "print $_SESSION['css'];" code, it seems to be actually passing the variable to the next page without problem....it SEEMS!

So what seems to be happening is that while the variable is getting passed, the code checking the variables to override the date isn't seeing it, and it's defaulting back. Can anyone look over my code and see if they can get why it's not passing the variable to the correct place, as well as making sure all my code is correct.

Thanks,

Jeff

Re: What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 3:31 am
by EverLearning
Of course you're gettting css according to date. Its in your conditions, which state(pseudo code)

Code: Select all

IF $_GET['css'] is not set
    use css determined by date
ELSE
    use $_GET['css']
So naturaly when you go to the bext page, $_GET['css'] will not be set and you'll get the css according to date.
You're saving $_GET['css'] in $_SESSION['css'], but you're not using it in your conditions.

Your code should be

Code: Select all

// so you dont get $_SESSION['css'] set to an empty value when $_GET['css'] isn't set
if (isset($_GET['css'])) {
    if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
        // set $_SESSION['css'] [b]ONLY[/b] if $_GET['css'] is equal to one of your predifined values
        $_SESSION["css"] = ($_GET['css']);
    }
}
 
//...
//...
//....and the css include conditions
 
if ($_GET['css']): ?>
// set css according to $_GET['css']
<?php elseif($_SESSION['css']):
// set css according to $_SESSION['css']
<?php else: ?>
// set css according to date
<?php endif: ?>
 

Re: What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 10:45 am
by crazylegsmurphy
Hey EverLearning,

I see what you're saying, but I am confused on a few things.

Firstly, would this code go into the index pages? And secondly, I think the code you wrote isn't correct, it seems to be missing something as it doesn't seem to render correctly (doesn't close tags).

Jeff

Re: What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 11:18 am
by crazylegsmurphy
Using (some) of the code above, the page how prints the session variable as per the code. And it seems to hold it even if I go away from that page, switch back and so forth. So in that respect the code is working. The major problem is that it's not relaying it back to the code to change the css, which I suspect is because the code above is written incorrectly.

Here is the code I currently have in the index.php file.

Code: Select all

<?PHP
session_start();
$section='community';
include('../_includes/top.php');
if (isset($_GET['css'])) {
if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
         $_SESSION["css"] = ($_GET['css']);
     }
}
 
print $_SESSION['css']; 
?>
 
<div id="content-preview">
            
            <p class="italic">Engage, Inspire...</p>
            <p><a class="more" href="community.php" title="community">More...</a></p>
            
            </div>
 
<?PHP
include('../_includes/bottom.php');
?>
           

Re: What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 4:26 pm
by EverLearning
crazylegsmurphy wrote: The major problem is that it's not relaying it back to the code to change the css,
I don't really understand what you mean. Can you explain more clearly whats not working? Explain what you want to happen.

Re: What is going on here? (Sessions - Variables)

Posted: Sun Jul 27, 2008 11:16 pm
by crazylegsmurphy
Well, basically...

The website has a css file for every season of the year (spring, summer, autumn, winter).

This means that depending on what time of year it is the website will detect the date and display the correct css file. This css file switches every image on the website to show photos for the season.

Right now, the code in the "top.php" file does this and it seems to be working perfectly.

The second thing that I need to have happen is that if the user decides they want to change the season of the website manually, the can do that by clicking links along the top (named: Spring, Summer, Autumn, Winter).

Right now, when you click the season links it actually changes to the correct css file. The problem is that it won't stay that way if you go to another page. When you go to another page, the website just defaults back to the current date and shows "summer" image.

So to sum up what I need in one sentence.... I need the website to display a different css file based on the date, but allow the user to override that and manually switch the season which will stay that season, until they leave the website.

Re: What is going on here? (Sessions - Variables)

Posted: Mon Jul 28, 2008 5:05 am
by crazylegsmurphy
solved it! Thanks all!