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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Login Session help

    I need a way of stoping a user on my website from accessing protected pages if they are not logged in, can anyone help? Im using the following code at the start of each page.

    Code:
    <?php
    session_start(); 
    include("database.php");
    include("loginemployer.php");
    ?>
    Last edited by PRodgers4284; 02-17-2008 at 12:36 PM.

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    you will need to add a few lines to the top of your script which check for a session. if it exists, go to the page. if not, redirect to login. I don't know the code, coz I use perl but, try googling for 'php sessions tutorials'.

    bazz

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bazz View Post
    you will need to add a few lines to the top of your script which check for a session. if it exists, go to the page. if not, redirect to login. I don't know the code, coz I use perl but, try googling for 'php sessions tutorials'.

    bazz
    Thanks for that

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Ive tried the code below but it doesnt same to work, not sure if im using the redirect header correctly

    PHP Code:
    if (!SESSION_username) {
        
    Header("Location: index2.php");
        } 

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    if(!isset($_SESSION['username'])){
    header("Location: /Login.php");

    try this?

  • #6
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    PHP Code:
    if(!isset($_SESSION['username'])){
    header("Location: /Login.php");

    try this?
    Got it working there thanks

    used this

    PHP Code:
    if( empty($_SESSION['username']) )
    {
        
    header("location: index2.php");


  • #7
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Well as you have seen. there are three session states,

    exists,
    empty
    doesn't exist

    You need to consider which states you need to check for. I think normally scripts check for 'isset' which looks for both empty and not empty sessions. remember just because session is empty doesn't mean it isn't set and so, a user can be logged in already but with an empty session. better to check for isset.


    bazz

  • #8
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bazz View Post
    Well as you have seen. there are three session states,

    exists,
    empty
    doesn't exist

    You need to consider which states you need to check for. I think normally scripts check for 'isset' which looks for both empty and not empty sessions. remember just because session is empty doesn't mean it isn't set and so, a user can be logged in already but with an empty session. better to check for isset.


    bazz

    Thanks for your advice, i think il change it now


  •  

    Posting Permissions

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