How to control session_start

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
gazzieh
Forum Commoner
Posts: 40
Joined: Wed May 19, 2010 7:46 am

How to control session_start

Post by gazzieh »

I have a site I am developing but I keep encountering the same issue.

The system uses a mySQL database structure to store user details and therefore I have created a PHP page that evaluates the login process.

This evaluation section is at the top of the index.php page as a require_once call. The actual webpage then loads, including the login box. Currently I simply want the system to evaluate as correct the log in and then return to the index.php page.

So, the code is working. I type in the username and password, click submit and it then reloads the index.php page. But I then get the error message:

Warning: Cannot modify header information - headers already sent by...

I know this is down to the reusing of session_start but how do I get the system to evaluate that the session_start has already been called and so not to do it again; avoiding the error altogether?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to control session_start

Post by Christopher »

You are getting that error because there is some output occurring before the session_start() is called. It can be a space or return before or after your <?php ?> in some file. Any output will force headers to be sent.
(#10850)
gazzieh
Forum Commoner
Posts: 40
Joined: Wed May 19, 2010 7:46 am

Re: How to control session_start

Post by gazzieh »

Christopher wrote:You are getting that error because there is some output occurring before the session_start() is called. It can be a space or return before or after your <?php ?> in some file. Any output will force headers to be sent.
Sorry, do not seem to have explained myself very well. This system works fine first time round BUT, once someone logs in the page needs to reload and, of course, a session_start has already been declared (as well as an entire webpage being sent across). How do I instruct the system NOT to send a new session_start once the person has logged in?

SESSION START
INDEX PAGE LOADS
USER LOGS IN
SYSTEM RELOADS INDEX PAGE
ERROR OCCURS HERE!
Post Reply