Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    If User Hasn't Logged In Don't Show Page

    I am editing a site where the person has set it up so even though pages can't be viewed, there is no session checking... This is what is currently is there...

    PHP Code:
    <?php 
        
    if($_SESSION['session_started'] != "TRUE")
        
    session_start();
    ?>
    I changed it to the following but it just keeps re-directing to the welcome.htm page...

    PHP Code:
    <?php 
        
    if($_SESSION['user_logged'] != "TRUE")
        
    header('Location: welcome.htm');
    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The $_SESSION global array won't exist unless you have session_start() at the top of the script.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So like so:

    PHP Code:
    <?php  
        
    if($_SESSION['user_logged'] != "TRUE")
        
    session_start(); 
        
    header('Location: welcome.htm'); 
    ?>

  • #4
    Banned
    Join Date
    Feb 2008
    Location
    Winnipeg, Canada
    Posts
    396
    Thanks
    0
    Thanked 29 Times in 29 Posts
    No, like this:

    PHP Code:
    <?php  
        session_start
    (); 
        if(
    $_SESSION['user_logged'] != "TRUE")
        
    header('Location: welcome.htm'); 
    ?>
    session_start(); should always be the first thing you do, unless of course one of your sessions in an object, then you need to define that object's class first - but I doubt you'll have to worry about that for quite some time

  • #5
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Try:
    PHP Code:

    <?php 
        
    if(!isset($_SESSION)) session_start();
        if(!
    $_SESSION['user_logged']) header('Location: welcome.htm');    
    ?>
    Of course this will only work providing that your logon script declares the var $_SESSION['user_logged'] with a boolean value properly. If the user has logged, $_SESSION['user_logged'] = TRUE; else $_SESSION['user_logged'] = FALSE.

    Check your logon script...
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by meth View Post
    Try:
    PHP Code:

    <?php 
        
    if(!isset($_SESSION)) session_start();
        if(!
    $_SESSION['user_logged']) header('Location: welcome.htm');    
    ?>
    Of course this will only work providing that your logon script declares the var $_SESSION['user_logged'] with a boolean value properly. If the user has logged, $_SESSION['user_logged'] = TRUE; else $_SESSION['user_logged'] = FALSE.

    Check your logon script...
    That first "if" statement will never be "false", so you may as well just code session_start();.

  • #7
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    PHP Code:
    <?php
    $session_msg 
    = (!isset($_SESSION)) ? 'session not set''session set';
    echo 
    $session_msg;
    ?>
    output = session not set

    PHP Code:
    <?php
    session_start
    ();
    $session_msg = (!isset($_SESSION)) ? 'session not set''session set';
    echo 
    $session_msg;
    ?>
    output = session set


    session_start() always returns true. The test isset($_SESSION) can go either way.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I took issue with checking isset($_SESSION) before the session_start() function was called (which seems to be what the OP is getting hung up on), that's all

  • #9
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    and if you "just use session_start()", you'll cause another error if it has already been called in an include; hence it's safer to call session_start() after the test for $_SESSION being set. Best to provide solutions that wont introduce new errors.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I guess if your includes are unknown black boxes, that's valid. Personally I keep an eye on what I include...

  • #11
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Personally I don't give a toss about your includes. I'd prefer not to make assumptions about the questioner's entire site based on a 3 line snippet.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

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