Click to See Complete Forum and Search --> : session variables


TxanTxan
November 28th, 2004, 06:06 AM
Hi!
I want to use session variables.
In the main page (main.php) I register the variable like this:

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css"/>
<title>Demo</title>
</head>

<body>
<?php
session_start();
$_SESSION['log'] = false;
?>
<div id="container">
<div id="header"><?php require('header.php'); ?></div>
<div id="topnav"><?php require('topnav.php'); ?></div>
<div id="sidenav"><?php require('sidenav.php'); ?></div>

...

Then, in the sidenav.php I look if the log variable is true or false:
<?php
if (!isset($_SESSION['log'])) {
echo("not defined");
}
else{
echo("defined");
}
if($_SESSION['log']==false){ /*not logged in*/
<form method="post" action=info.php?what=login>
<br>Username: <input class="inputStyle" type=text name="username"></br>
<br>Password: <input class="inputStyle" type=password name="password"></br>
<br><input class="submitStyle" type=submit value="Login"></br>
</form>
...

}
else { /*logged in*/

...
}

This sidenav is included in the main.php and it works in this main page. But when I jump to another page that also includes the sidenav.php (for example, when I click the Login button and I go to info.php) the log session variable is not defined. Why it isn't defined in the other files?

Thanks

accessxtreme
November 28th, 2004, 09:28 AM
In your sidenav make sure you have a session being started. Not in your main page. It will only work on your main page since that is the only place you have the session started. I had this problem before and that is what I did to correct it. I am not sure if it is going to work for yours but it looks like that is the only thing wrong with the code you posted. Try and switch them and see the results.


Jason

Viper_SB
November 28th, 2004, 07:22 PM
Yes accessxtreme is right you need to include session_start() in ALL your files that you wish to use session variables (an include works nicly) OR you can set session.auto_start = 1 in your php.ini and it'll automaticly start a session on ALL your pages (not always what you want)