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
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    help with $_SESSION

    I need some helping getting my session to work. Basically, I have a login script where a user types in a username and password and logs into the site. In my database, I have a field called "status" and it shows whether the user who logged in is an Administrator or Moderator.
    What I want is when the person logs in, on the next page that is displayed, I want certain data to be displayed only if a person is an Administrator, and not a moderator. I figured I can do this best with using $_SESSION, but I haven't had much success with it. Here's what I have so far:

    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_SESSION['status']) == Administrator)
    {
    echo 
    "Hi!";
    }
    else {
    echo 
    "Bye!";
    }
    ?>
    So if the user who logged in is an admin, then "Hi!" should appear, if not then "Bye!" should appear.

    Here's my authentication script for the login system:

    PHP Code:
    $username mysql_real_escape_string($_POST['username']);
    $password mysql_real_escape_string($_POST['password']);

    $sql "SELECT * FROM admins WHERE username='$username' and password='$password'";
    $result mysql_query($sql);
    $status "SELECT status FROM admins WHERE username='$username'";

    $count mysql_num_rows($result);

    if(
    $count == 1){
    $_SESSION['username'] = $username
    $_SESSION['password'] = $password;
    $_SESSION['status'] = $status;
    $_SESSION['db_logged_in'] = true;
    header("location:login_success.php");
    }
    else  {
    echo 
    "blah blah blah";

    I need help getting this sorted out. My code works but it displays "Hi!" no matter what the status of the user is.

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    just try doing this :

    <?php
    session_start();
    if(isset($_SESSION['status']) && ($_SESSION['status'] == "Administrator"))
    {
    echo "Hi!";
    }
    else {
    echo "Bye!";
    }
    ?>

    hope this works

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Unfortunately, that is not working. I'm now receiving the "Bye!" message on a user that has a status of Administrator.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    var_dump($_SESSION['status']); on the page you're getting the Bye message.

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    May I ask where exactly I place the var_dump($_SESSION['status']); code in the Bye page?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Anywhere after session_start() and before the if/else code above.

  • #7
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So something like this?

    PHP Code:
    <?php
    session_start
    ();
    var_dump($_SESSION['status']);
    if(isset(
    $_SESSION['status']) && ($_SESSION['status'] == "Administrator"))
    {
    echo 
    "Hi!";
    }
    else {
    echo 
    "Bye!";
    }

    ?>
    Because all that is doing is displaying

    string(50) "SELECT status FROM admins WHERE username='Fox'" Bye!

    on the page, which I clearly don't want.

  • #8
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey try this code.

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $sql = "SELECT * FROM admins WHERE username='$username' and password='$password'";
    $result = mysql_query($sql);
    $rs = mysql_fetch_assoc($result);

    $_SESSION['status'] = $rs['status'];



    Quote Originally Posted by Bob42 View Post
    I need some helping getting my session to work. Basically, I have a login script where a user types in a username and password and logs into the site. In my database, I have a field called "status" and it shows whether the user who logged in is an Administrator or Moderator.
    What I want is when the person logs in, on the next page that is displayed, I want certain data to be displayed only if a person is an Administrator, and not a moderator. I figured I can do this best with using $_SESSION, but I haven't had much success with it. Here's what I have so far:

    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_SESSION['status']) == Administrator)
    {
    echo 
    "Hi!";
    }
    else {
    echo 
    "Bye!";
    }
    ?>
    So if the user who logged in is an admin, then "Hi!" should appear, if not then "Bye!" should appear.

    Here's my authentication script for the login system:

    PHP Code:
    $username mysql_real_escape_string($_POST['username']);
    $password mysql_real_escape_string($_POST['password']);

    $sql "SELECT * FROM admins WHERE username='$username' and password='$password'";
    $result mysql_query($sql);
    $status "SELECT status FROM admins WHERE username='$username'";

    $count mysql_num_rows($result);

    if(
    $count == 1){
    $_SESSION['username'] = $username
    $_SESSION['password'] = $password;
    $_SESSION['status'] = $status;
    $_SESSION['db_logged_in'] = true;
    header("location:login_success.php");
    }
    else  {
    echo 
    "blah blah blah";

    I need help getting this sorted out. My code works but it displays "Hi!" no matter what the status of the user is.
    -------------------------
    Shailesh Patil
    -------------------------


  •  

    Posting Permissions

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