Session & if question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aronick
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 9:45 pm

Session & if question

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
?>
Aronick
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 9:45 pm

Post by Aronick »

It works just how I want it to, thank-you so much :)
Aronick
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 9:45 pm

Post 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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

session_destroy()
Aronick
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 9:45 pm

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
Aronick
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 9:45 pm

Post 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.
Post Reply