using session values from different pages
Moderator: General Moderators
using session values from different pages
Hi,
I'm new in php and generally in programming. I'm trying to use sessions for the log in. While logging in a session stores a value for the username, and then the index page cheks if the username is still stored or is timed out.
The problem is that from the index page I can not use this session at all. How can I get the values from the same session from different pages??
Thanks alot!!!
I'm new in php and generally in programming. I'm trying to use sessions for the log in. While logging in a session stores a value for the username, and then the index page cheks if the username is still stored or is timed out.
The problem is that from the index page I can not use this session at all. How can I get the values from the same session from different pages??
Thanks alot!!!
Re: using session values from different pages
On any page you want to access previous session data, you have to include a call to session_start before trying to read the session variables. Since you didn't post any code I'm just guessing because that is a common error.
Re: using session values from different pages
Thanks for your reply!!
I called in both pages session_start() but I still cant access the data.
Here is some code:
the php file that creates and stores the session (login_session.php):
and the index.php:
I get this warning message : Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files .....
Also it echo 's only the "session timedout" or "session not set" messages I 'm using(what I mean: I refresh the page and I have "session timedout", I refresh again and I have "session not set", then again "session timedout" and so it goes on).
Hope I was clear enough...
I called in both pages session_start() but I still cant access the data.
Here is some code:
the php file that creates and stores the session (login_session.php):
Code: Select all
<?php
session_start();
$_SESSION['uname'] = "myUsername";
?>Code: Select all
<?php
session_start();
// set timeout period in seconds
$inactive = 600;
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive)
{
echo "<br /><b>session timedout...</b>";
session_destroy();
}
else {echo $_SESSION['uname']; }
}
else {echo "<br /><b>session not set...</b>"; }
$_SESSION['timeout'] = time();
?>Also it echo 's only the "session timedout" or "session not set" messages I 'm using(what I mean: I refresh the page and I have "session timedout", I refresh again and I have "session not set", then again "session timedout" and so it goes on).
Hope I was clear enough...
Re: using session values from different pages
I see C:\Program Files in yr post .....have you tested this on a live server?lyros wrote:Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files .....
Re: using session values from different pages
Put your php code before your html and don't echo anything before calling header(). Make sure the php is at the very top of the page.lyros wrote:I get this warning message : Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files .....
Check to see if your variables are correct by
Code: Select all
echo '<pre>'.var_dump($_SESSION).'</pre>';Re: using session values from different pages
I just realized why it shows the warning message. If it has timed out I want it to redirect to the login.php page:
if (!isset($_SESSION['uname'])){
header("Location: login.php");
}
I assume that session_start() sends headers to the server, and I also send later on another header for redirection. So is there a way to compine these two (session_start and header) ?
if (!isset($_SESSION['uname'])){
header("Location: login.php");
}
I assume that session_start() sends headers to the server, and I also send later on another header for redirection. So is there a way to compine these two (session_start and header) ?
Re: using session values from different pages
You can put the:
after:
Code: Select all
header("Location: login.php");Code: Select all
session_start();Re: using session values from different pages
Like danielrs1 says, session_start should not effect your header redirect. I put the header redirect after session start all the time. The problem is you're probably sending html of some sort which would cause the header portion of i/o to change to html and not allow anymore header commands. If you post your full code (including the surrounding html) it would be easier to see what is happening.lyros wrote:I assume that session_start() sends headers to the server, and I also send later on another header for redirection. So is there a way to compine these two (session_start and header) ?
Re: using session values from different pages
Thanks, you were both right guys. Did n't notice that I had some echo's before the header...
My bad!!!
Thanks alot!!!
My bad!!!
Thanks alot!!!
Re: using session values from different pages
No prob.
In the future remember posting the full code can save us a lot of guessing. Unless the code is huge of course....