SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist Fleeters's Avatar
    Join Date
    Jul 2003
    Location
    Dumpsville
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing if Sessions have correctly started.

    I was reading on the php docs that session_start() always returns true. What is the best way to test that they actually started?


    thanks
    Aaron Smith
    smithaaronlee.net

  2. #2
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(session_start() == TRUE){
        Echo 
    "Yay";
    }
    else{
        Echo 
    "You Suck";


  3. #3
    SitePoint Evangelist Fleeters's Avatar
    Join Date
    Jul 2003
    Location
    Dumpsville
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on the PHP docs it says this though:

    "This function always returns TRUE."
    Aaron Smith
    smithaaronlee.net

  4. #4
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because it is starting a session, I can't think of a reason that using session_start() would fail unless it was in some sort of class or loop but then it would be a problem with the class or the loop.

    Well maybe if cookies were disabled on the machine that might cause it to fail, but that is also dependent upon how you have your PHP.ini setup.

    After testing it with cookies disabled it still creates the session and you can store things in the session array, but it will not propagate to another page. The session information dies when you leave the page you created the session on.

  5. #5
    SitePoint Evangelist Fleeters's Avatar
    Join Date
    Jul 2003
    Location
    Dumpsville
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well my host was having problems with the session tmp directory not being there.

    That was causing sessions to fail. I now need to implement a session monitor so that if that happens again I am notified. So now I just need an accurate way of finding out whether or not sessions_start failed.
    Aaron Smith
    smithaaronlee.net

  6. #6
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just run a check on the director then and verify proper permissions rather than verifying the session_start itself.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this alone cant tell you if the session cookie was sent, nor if the user accepts cookies, nor if url rewriting was successful if session.use_trans_sid is on, but it will tell you if php can write to a file.

    PHP Code:
    session_start();
    $_SESSION['test'] = true;
    session_write_close();
    $_SESSION = array();
    session_start();
    $success = isset($_SESSION['test']); 

  8. #8
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Testing sessions??

    Clamcrusher,

    I'm pretty new to sessions. After running that code, how do you find out whether it actually worked?

    PHP Code:
    session_start();
    $_SESSION['test'] = true;
    session_write_close();
    $_SESSION = array();
    session_start();
    $success = isset($_SESSION['test']); 

  9. #9
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($success == true) {
    echo 
    'works';
    }else{
    echo 
    'doesnt work';

    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


Bookmarks

Posting Permissions

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