www.webdeveloper.com
Results 1 to 5 of 5

Thread: when to use session_start()?

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    57

    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
    Location
    Canada
    Posts
    155
    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_start();
    if(!isset(
    $_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
    Posts
    57
    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
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,361
    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
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    752
    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.

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



Recent Articles