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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool session state help

    this is part of the code that i have on my login screen
    PHP Code:
    $sql="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword,table is 1 row
    if($count==1){

    $_SESSION['myusername'] = $myusername;
    header("location:.........");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?> 
    and this is the other part of the code where i want it so that if a user is not logged in that they will be redirected to the login page to either register their account or login could someone give me some help as to what i'm doing wwong here since it still lets you access the page without being logged on
    PHP Code:
    <?PHP
    session_start
    ();

    if(isset(
    $_SESSION['myusername'])){ 
    //session is set, user is logged in 
    }else{ 
    header("location:........."); 
    }

    ?>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    else { 
        unset(
    $_SESSION['myusername']);
        echo 
    "Wrong Username or Password"

    ..is one way. Or, preferably:
    PHP Code:
    else {  
        if (isset(
    $_SESSION['myusername'])) {
            unset(
    $_SESSION['myusername']);
        }
        echo 
    "Wrong Username or Password";  

    Last edited by AndrewGSW; 12-13-2012 at 01:59 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you are still able to open the other page without being logged on since it don't redirect to the login page if not logged in

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i managed to get it to redirect but it don't when i type in the correct username and password and click on the page it still redirects me to login page

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Location
    Toronto, ON
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dekon View Post
    this is part of the code that i have on my login screen
    PHP Code:
    $sql="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword,table is 1 row
    if($count==1){

    $_SESSION['myusername'] = $myusername;
    header("location:.........");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?> 
    and this is the other part of the code where i want it so that if a user is not logged in that they will be redirected to the login page to either register their account or login could someone give me some help as to what i'm doing wwong here since it still lets you access the page without being logged on
    PHP Code:
    <?PHP
    session_start
    ();

    if(isset(
    $_SESSION['myusername'])){ 
    //session is set, user is logged in 
    }else{ 
    header("location:........."); 
    }

    ?>
    Hi Decon,

    1)
    Did you try to delete your cookies. press Ctrl + Shift + Del and then re-try same thing.

    2)
    Also try if($count=='1') instead of if($count==1)

    3)
    Try to echo your sql queries. This will get you valuable information.

    Let me know what happens.

    Regards,
    Nehal


  •  

    Posting Permissions

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