Jump to content

[SOLVED] What is going on here? (Sessions - Variables)


crazylegsmurphy

Recommended Posts

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:

 

<?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:

 

<?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

If I do that, then the print function stops showing the session, and it still doesn't load the correct page.

 

You can all see it here http://www.mmmyeah.com/community/

 

If you first click "Autumn" on the top you will see that "Session is: Autumn" appears beside those links and the css will change. If you then click "more..." then the css defaults back (you can see this if you click "autumn" again in that second page.

 

Edit: To add, if I add those "session_start () into the index.php page, then the print code "print $_SESSION['css'];" will display "autumn" on the left...which means (from what I can tell) it's actually passing the variable....but again, the css is defaulting back.

 

Try this.

 

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:

 

<?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:

 

<?php
include('../_includes/top.php'); // <---- INCLUDE THE TOP FILE BEFORE YOU ECHO THE SESSION
print $_SESSION['css']; 

$section='community';

?>

<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

 

As for the other problem, didn't you ask this in another topic?

I did, ask this yes, but It was all mashed in between another question and the code was all over the place. This is basically a cleaned up version of the question. Once this one is solved I'll then post in the other one a link to this topic and post them both as solved.

 

Now on to try what you posted. If you don't mind, could you explain why you did what you did in the first one? I would like to understand the reason behind that.

 

Thanks,

 

Jeff

Someone else e-mailed me and suggested this.

 

<   1. // so you dont get $_SESSION['css'] set to an empty value when $_GET['css'] isn't set
   2. if (isset($_GET['css'])) {
   3.     if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
   4.         // set $_SESSION['css'] ONLY if $_GET['css'] is equal to one of your predifined values
   5.         $_SESSION["css"] = ($_GET['css']);
   6.     }
   7. }
   8.  
   9. //...
  10. //...
  11. //....and the css include conditions
  12.  
  13. if ($_GET['css']): ?>
  14. // set css according to $_GET['css']
  15. <?php elseif($_SESSION['css']):
  16. // set css according to $_SESSION['css']
  17. <?php else: ?>
  18. // set css according to date
  19. <?php endif: ?>
  20.  

 

It seems to make sense that my initial code wasn't even correct, but I don't think this is either, can someone look at it and see what's wrong with it?

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.

 

<?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');
?>
            

 

 

is this really difficult or something? I don't mean to sound like a jerk, but I'm sure this is pretty basic stuff...if someone could just take a look. 50 people have looked at this thread, someone has to see what the problem is.

 

I know I'm being impatient, but the sooner I can get this solved the sooner I can be done with this site. It's basically the last thing keeping me from giving it to the client.

I'll try and save the day :P I seen this post here for like 2 days now straight. About time it got solved ;D

 

try this:

<?php
session_start();

error_reporting(E_ALL);

$section='community';
include('../_includes/top.php');
if (isset($_GET['css']) && !empty($_GET['css'])) {
if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
        $_SESSION['css'] = $_GET['css'];
    }
}

echo $_SESSION['css']; 
?>

 

Now lets see how that fares. Good Luck.

 

Question, your just echoing the value of $_GET, but in a $SESSION variable. So is the CSS in there own files and by the name of spring, summer, autumn and winter?

 

Regards ACE

Parse error: syntax error, unexpected '&' in /home/mmmyeahc/public_html/community/index.php on line 10

 

Hehe...close you almost saved the day! :D

 

Basically there are 5 css files. One is called "screen" which holds all the styles that relate to the site across all seasons, then each css file (spring, summer) has the style to change the background images to the correct season.

 

You can see if you go to http://www.mmmyeah.com/index1.php if you look at the source, you'll see it's first loading screen.css, and then summer.css based on the code saying to do that because it's between the summer months. If you then click at the top to change the season, it will change the image (load the correct css file). That all works perfectly, but then if you go anywhere else, it defaults back to summer.

It should, but it's defaulting back for some reason.

 

Even when I print the session it's staying the same across the pages, but the code to switch it just isn't getting the variable or something. It's like it's not even seeing it.

 

so your code should be?

 

<?php
session_start();

error_reporting(E_ALL);

$section='community';
include('../_includes/top.php');
if (isset($_GET['css']) && !empty($_GET['css'])) {
if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
$_SESSION['css'] = $_GET['css'];
}
}

echo $_SESSION['css']; 
?>

It's doing the same...

 

It's showing "Autumn" when I click the "Autumn" link...and if I switch between the two pages I put this code on, it shows the session is still "Autumn"....but the css file is summer. It will switch initially when I click the link, but it'll go back to summer, even though the "autumn" variable is being printed by the session.

I believe the problem is with doing this on every page...

if (isset($_GET['css']) && !empty($_GET['css'])) {
if (in_array($_GET['css'], array('spring', 'summer', 'autumn', 'winter'))) {
$_SESSION['css'] = $_GET['css'];
}
}

 

Put that on say, the login page or whatever. Then everything should be right.

Well there really isn't a "login" page. The pages are "built" server side when the index.php file is loaded.

 

The index page(s) load first, then they have includes which build the top and bottom php files around it. I am starting to suspect the problem is with how the pages are loading...or more so the order.

 

I'll try putting the code into the top.php file right now and see what happens.

Can I make a suggestion? Try reading the PHP errors, and see if it helps. Chances are if it's an unexpected character on line x, the problem will be on line x with the unexpected character :/

 

----------------

Now playing: Enter Shikari - No Sssweat

via FoxyTunes

Ok..

 

It's meant to do three things.

 

1. Load a css file based on the date (season of the year)

2. Allow the user to manually change the css file (to the season, Spring, Summer, etc.)

3. The season the user picks needs to stay that season as they browse the site, and then default back to the date when they return at a later date.

<?php

$seasons = array (
'spring' => array('March', 'April', 'May'),
'summer' => array('June', 'July', 'August'),
'autumn' => array('September', 'October', 'November'),
'december' => array('December', 'January', 'February')
);

if(isset($_GET['css']) && array_key_exists($_GET['css'], $seasons)) {
$css = $_GET['css'];
} else {
foreach($seasons as $season) {
if(in_array(date('F'), $season)) {
$css = $season;
}
}
}

/* CSS stuff */
echo "<link rel=\"stylesheet\" href=\"/styles/$css.css\">";

?>

 

NOTE: This assumes that your stylesheets are in a /styles/ folder and are named spring.css, summer.css, autumn.css and winter.css

 

----------------

Now playing: Enter Shikari - Return To Energizer

via FoxyTunes

They are in /_stylesheets/

 

and ya, they're named winter.css etc.

 

Can someone test this for me? http://www.mmmyeah.com/community/

 

Click "Autumn" at the top (the picture will change), then click "More..." in the middle...it should be a photo of a rock...not a flower. If it's a flower, it's defaulting back to summer.css.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.