Page 1 of 1
using session values from different pages
Posted: Thu Jul 02, 2009 9:05 am
by lyros
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!!!
Re: using session values from different pages
Posted: Thu Jul 02, 2009 9:12 am
by Eric!
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
Posted: Thu Jul 02, 2009 9:52 am
by lyros
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):
Code: Select all
<?php
session_start();
$_SESSION['uname'] = "myUsername";
?>
and the index.php:
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();
?>
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...
Re: using session values from different pages
Posted: Thu Jul 02, 2009 10:07 am
by wayneob
lyros wrote:Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files .....
I see C:\Program Files in yr post .....have you tested this on a live server?
Re: using session values from different pages
Posted: Thu Jul 02, 2009 10:15 am
by Eric!
lyros wrote:I get this warning message : Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files .....
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.
Check to see if your variables are correct by
Code: Select all
echo '<pre>'.var_dump($_SESSION).'</pre>';
after your session_start().
Re: using session values from different pages
Posted: Thu Jul 02, 2009 11:54 am
by lyros
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) ?
Re: using session values from different pages
Posted: Thu Jul 02, 2009 12:21 pm
by danielrs1
Re: using session values from different pages
Posted: Thu Jul 02, 2009 3:56 pm
by Eric!
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) ?
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.
Re: using session values from different pages
Posted: Sat Jul 04, 2009 5:27 am
by lyros
Thanks, you were both right guys. Did n't notice that I had some echo's before the header...
My bad!!!
Thanks alot!!!
Re: using session values from different pages
Posted: Sat Jul 04, 2009 5:56 am
by Eric!
No prob.

In the future remember posting the full code can save us a lot of guessing. Unless the code is huge of course....