You are invited to Log in or Register a free Frihost Account!

php SESSIONS question

I've been trying to work out how to use sessions & I've looked through tutorials and topics here, and now I'm even more confused.

In the tutorial I'm using it says about using session_start() and session_register(). The way I read it is that session_start() actually starts a new session. Yet in a topic in this forum it says to put session_start() at the top of each page. Won't that start a new set of session variables?
You should indeed place it at the top of every page taking part in the session. session_start() either starts or resumes a session.

It basically loads the session variables into the environment, adds a handler to the page output (which will make sure to add the session ID to the end of URLs in your code, if not using cookies) etc. So, since one PHP page doesn't actually know what the previous one did, we tell it to keep the session up by calling session_start().

The session variables won't be cleared, because you got the current session ID in through the url or a cookie.
As Serge said, session_start() is a bit of a misnomer because it also resumes if there is a session logged for that user already.

Remember that you have to start(/resume) a session before you do any output to the browser! That is the reason why the tutorial suggests putting it at the top of every page. Personally it's a lot easier to include() a header.php file and you can stick all the HTML <head> contents and doctype in at the same time.
Thanks both of you for clearing that up for me. Very Happy
It all works perfectly now.

So, that's the membership system complete; now I can get on with the real meat! Hopefully I won't have too much trouble with that. The logic and database stuff is a bit complex, but nothing I don't know how to handle (famous last words, eh!). When I get a bit more proficient with php I dare say I'll see easier & more elegant ways to do things, but that can wait til later.

Thanks again.
Related topics
php sessions
PHP: Sessions and Cookies
PHP Editing Question
PHP Sessions
php sessions
PHP magic_quotes_gpc question
Please help with php-quick question
PHP sessions and security
php sessions for different accounts
php, css question (wordpress)
php nuke question
php/sql question
The last foolish PHP/MySQL Question of the Year....
PHP Session Timeout/Max Life Time
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.