Page 1 of 1

Session & if question

Posted: Thu Jan 29, 2004 9:45 pm
by Aronick
Okay now I'm trying to setup my site to have 2 different layouts. Now currently the site is programmed in php.

What I was thinking about doing was if someone wanted to change the layout they could simply click a link and it would set a cookie or I believe a session(?) as it's called in php. Now from there whenever someone comes to the site inside the index.php there would be an if statement that would look something like this:

<?php

if()
{
include("top.shtml");
include("middle.shtml");
include("bottom.shtml");
}
else
{
include("top2.shtml");
include("middle2.shtml");
include("bottom2.shtml");
}

?>

Now I don't know if this would work, but in theory I think it would. I'm just wondering what code would I need to make a link to create a session, and then from there what statement would I need to have for the if statement to work?

Thanks.

Posted: Fri Jan 30, 2004 2:32 pm
by DuFF
To work with sessions:

Index Page:

Code: Select all

<?php
session_start();
if($_SESSION['style2'])  //if session variable = 2
{
include("top2.shtml");
include("middle2.shtml");
include("bottom2.shtml");
}
else
{
include("top.shtml");
include("middle.shtml");
include("bottom.shtml");
echo "<a href="changestyle.php">Change the style!</a>";
}
?>
Then on changestyle.php:

Code: Select all

<?php
session_start();
$_SESSION['style2'] = "TRUE";  //set session to TRUE
header("Location: index.php");  //redirect back to index.php
?>

Posted: Fri Jan 30, 2004 4:41 pm
by Aronick
It works just how I want it to, thank-you so much :)

Posted: Fri Jan 30, 2004 6:41 pm
by Aronick
One last question, how do I go about deleting the session? I tried setting the TRUE in the code below to FALSE but it didn't work.

$_SESSION['style2'] = "TRUE"; //set session to TRUE

Posted: Fri Jan 30, 2004 6:49 pm
by Unipus
session_destroy()

Posted: Fri Jan 30, 2004 7:21 pm
by Aronick
And where would I put that code? Becuase I replaced it where session_start() was but it is giving me this error now:

Warning: Trying to destroy uninitialized session in /home/break.php on line 2

Warning: Cannot add header information - headers already sent by (output started at /home/break.php:2) in /home/break.php on line 4

Posted: Fri Jan 30, 2004 8:24 pm
by DuFF
Straight from PHP.net:
Example 1. Destroying a session

Code: Select all

<?php

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
session_unset();
// Finally, destroy the session.
session_destroy();

?>
Example 2. Destroying a session with $_SESSION

Code: Select all

<?php

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// Finally, destroy the session.
session_destroy();

?>
Use the second one.

Posted: Tue Feb 03, 2004 9:08 pm
by Aronick
Okay hopefully one last thing.

How do I make it so that the session stays for a specific amount of time like a cookie? I looked and tried few things on php.net but I couldn’t seem to find out how to do it.

Thanks.