when to use session_start()?
When should the session_start function be used and what exactly does it do? I read the section in the php manual and some stackoverflow but i don't have a clear understanding of it
Should I use session_start() on every .php page including the home page or just like the login page?
session_start() is meant to be used on any page that will in some way access a $_SESSION variable, whether that means reading the information in it, setting information, unsetting the variable, etc... . When I use it, I always place session_start() at the very top of my page, just so I keep all of my pages somewhat standardized and organized. If the page does not contain session_start(), then you're not going to have access to the $_SESSION variables. In other words, if you have a login page but no session_start() on other pages, then unless you have some other security measure in place, anyone can directly go to that page even if they're not logged in. Depending what you're making, you may not want this. For example:
The above checks whether the $_SESSION variable for the user name is set, meaning the person is logged in. If it's not set, then either the user isn't logged in or there was an error setting the variable. Having this on your home page depends on exactly how you want your home page to behave and whether the content should change based on the user who is logged in.
// redirect to log-in
Note that if you're using $_SESSION variables, you can still use $_GET and $_POST variables, they will not conflict with each other. To avoid confusion, try to make sure that none of your $_SESSION variables use the same index names as your $_POST variables (i.e. so you don't have $_POST['test'], $_SESSION['test'] and $_GET['test']).
would there be a problem if I used session_start() on a page that wouldn't be using a $_SESSION variable?
Just make a part of your normal template which you use to develop pages. That way it is always there and should you add a session var later on you don't have to figure out why it isn't working.
The only "problem" would be the few microseconds* of additional processing time, which 99.99999%* of the time should be of no concern.
Originally Posted by ytesfay80
* 63.7% of all statistics are made up
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread