'$_session's Dont Make Sense!!!!!!

Discussion in 'PHP' started by robertl, Jul 3, 2007.

  1. #1
    Look at the following code:

    Is there something wrong with this picture when it includes header.php and echoes $_SESSION['hs_un']????
     
    robertl, Jul 3, 2007 IP
  2. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That block of code is really bad... what is this from? It depends on what the session is really. If its really sensitive info, I don't think you would want to display it like that.
     
    xxKillswitch, Jul 3, 2007 IP
  3. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    if ($_SESSION['hsb_un'] != "") {
    include('includes/headerb.php');
    }
    else
    if ($_SESSION['hs_un'] != "") {
    include('includes/headers.php');
    }
    else
    {
    include('includes/header.php');
    echo $_SESSION['hs_un'];
    }
    
    Code (markup):
    I don't know why you have an echo there. It only includes header.php when hs_un is an empty string.
     
    KalvinB, Jul 3, 2007 IP
  4. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should use something like
    
    if (!isset($_SESSION['hsb_un']))
    {
      include('includes/headerb.php');
    }
    
    Code (markup):
    or
    
    if (empty($_SESSION['hsb_un']))
    {
      include('includes/headerb.php');
    }
    
    Code (markup):
    That is if you don't get any results. Also don't forget about session_start() before printing anything or calling a $_SESSION variable.
    I don't get it, when $_SESSION['hsb_un'] == "" && $_SESSION['hs_un'] == "" it doesn't include the header.php file or what? You are telling the script to print $_SESSION['hs_un'] only if $_SESSION['hs_un'] == "", ie when it is has an empty value. It will not echo anything, what would you expect?
     
    SEV3N, Jul 3, 2007 IP
  5. robertl

    robertl Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The only reason i put 'echo' is for testing reasons. 'header.php' should be shown if there is no value for $_SESSION['hsb_un'] and $_SESSION['hs_un']. This is not sensitive information. I have reached this 'bad form', because using (!)empty() and (!)isset() did not seem to work, so i am thinking it is a session problem. I do have session_start(); could output buffering be the problem?
     
    robertl, Jul 3, 2007 IP
  6. robertl

    robertl Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the problem is that $_SESSION['hs_un'] does echo and have a value, even thought the print statement is inside a conditional 'if' that requires there be no value for it. Thats the part that doesnt make sense to me!
     
    robertl, Jul 3, 2007 IP
  7. robertl

    robertl Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay, now i have used isset(). I put !isset($_SESSION['hs_un']) on the 'if' that allows header.php. And then i put isset($_SESSION['hs_un']) on the conditional that allows headers.php. Both show!!!!!! what is going on!!!!?????? $_SESSION['hs_un'] returns true on both isset and !isset!!!!!!!!!!!!!!1
     
    robertl, Jul 3, 2007 IP
  8. robertl

    robertl Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok guys nevermind; its fixed. The session_start() was in the headers, for which i was trying to determine the presence of a $_SESSION[''] value.
     
    robertl, Jul 3, 2007 IP
  9. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It is good practice to place session_start() on main pages, not on include pages. In your case you had to read a session variable, then load the header that contained session_start() and that's impossible. Maybe next time give some more details.
    Good luck with your project!
     
    SEV3N, Jul 3, 2007 IP