SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Log-In Box

  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Log-In Box

    Hiya,

    I'm new to PHP, how do I make a simple Login page that compares the Username and Password entered in a form, with whats in a table.

    If login is unsuccessful, how do I display an error message.

    If login is successful, how do I navigate to a different page, and keep the user logged-in until the user chooses to logout?
    Last edited by Salchester; Jul 2, 2007 at 12:10.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    session_start();
    mysql_connect("localhost""username""password");
    mysql_select_db("some_db");

    $username $_POST['username'];
    $password $_POST['password'];

    $sql "SELECT id FROM users WHERE username = '" mysql_real_escape_string($username) . "' AND password = '" mysql_real_escape_string($password) . "'";
    $result mysql_query($sql);
    if (
    mysql_num_rows($result) == 0) {
      echo 
    "Username and password combination not found.";
    } else {
      
    $row mysql_fetch_array($result);
      
    $_SESSION['logged_in'] = 1;
      
    $_SESSION['user_id'] = $row['id'];
      
    header("Location: http://www.example.com/member-area.php");

    On your pages that require authorization, check for the presence of those session values:

    PHP Code:
    session_start();
    if (!isset(
    $_SESSION['logged_in'])) {
      
    //Not logged in, redirect to login page
      
    header("http://www.example.com/login.php");
    }
    //If they pass that, they're logged in, and are authorized to view whatever else is in this file 

  3. #3
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logout Link

    How do I displayed the users Username on every page once the user has logged in, along with a "Logout" link, that upon being clicked logs the user out of the system resulting in the user having to log back in again to return to the members area?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Add the username to the query and session array in the login code, then echo that variable on your other pages. The logout page needs just this code:

    PHP Code:
    session_start();
    unset(
    $_SESSION['logged_in']); 

  5. #5
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Log-In Box

    Add the username to the query and session array in the login code, then echo that variable on your other pages.
    How do I do that, as I'm new to PHP & sessions?

    So the logout page only needs the following code? Doesn't the session need to be destroyed to logout, in order for the user to have to re-login?

    Code PHP:
    session_start();
    unset($_SESSION['logged_in']);

  6. #6
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Log-In Box

    Upon running the following code, I get greeted with the following error message:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
    C:\Documents and Settings\It's Dale\Desktop\xampplite\htdocs\Login.php on line 21

    Code:
    <?php
    
    session_start();
    
    mysql_connect("localhost", "peter", "abc123");
    
    mysql_select_db("Users");
    
    
    
    $Username = $_POST['Username'];
    
    $Password = $_POST['Password'];
    
    
    
    $sql = "SELECT id FROM users WHERE Username = '" . mysql_real_escape_string($username) . "' AND password = '" . mysql_real_escape_string($password) . "'";
    
    $result = mysql_query($sql);
    
    if (mysql_num_rows($result) == 0) {
    
      echo "Username and password combination not found.";
    
    } else {
    
      $row = mysql_fetch_array($result);
    
      $_SESSION['logged_in'] = 1;
    
      $_SESSION['user_id'] = $row['id'];
    
      header("Location: http://www.example.com/member-area.php");
    
    } 
    ?>

  7. #7
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dont u need to use the mysql connect link which is $con or watever

    then just replace it cuz looks like its not connectingg to database

  8. #8
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by william232 View Post
    dont u need to use the mysql connect link which is $con or watever

    then just replace it cuz looks like its not connectingg to database
    What do you mean? Replace what?

  9. #9
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so its like

    $con=mysql_connect("localhost", "peter", "abc123");

    mysql_select_db("Users")

    then on querys have it

    $result = mysql_query($sql,$con);

    Try that.

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by william232 View Post
    so its like

    $con=mysql_connect("localhost", "peter", "abc123");

    mysql_select_db("Users")

    then on querys have it

    $result = mysql_query($sql,$con);

    Try that.
    mysql_query() will use the last opened link if that parameter is omitted.

  11. #11
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Log-In Box

    I'm new to PHP, how do I display the word "Log-Out" on the screen, which upon being clicked, logs the user out of the system. Requiring the user to re-login to review the members page?

  12. #12
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is the way i do it dan


Bookmarks

Posting Permissions

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