Results 1 to 5 of 5

Thread: when to use session_start()?

  1. #1
    Join Date
    Dec 2013

    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?

  2. #2
    Join Date
    Feb 2014
    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:

    PHP Code:
    $_SESSION['username'])) {
    // redirect to log-in

    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.

    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']).

  3. #3
    Join Date
    Dec 2013
    would there be a problem if I used session_start() on a page that wouldn't be using a $_SESSION variable?

  4. #4
    Join Date
    Jul 2013
    Voorheesville NY USA
    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.

  5. #5
    Join Date
    Aug 2004
    Quote Originally Posted by ytesfay80 View Post
    would there be a problem if I used session_start() on a page that wouldn't be using a $_SESSION variable?
    The only "problem" would be the few microseconds* of additional processing time, which 99.99999%* of the time should be of no concern.
    * 63.7% of all statistics are made up
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center