Use database to store user name and password : Authentication « HTML « PHP






Use database to store user name and password

 
//login.html
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Username:<br /><input type="text" name="username" size="10" /><br />
        Password:<br /><input type="password" name="pswd" SIZE="10" /><br />
        <input type="submit" value="Login" />  
    </form>

//index.php
<?php
   session_start();
   if (! isset($_SESSION['name'])) {
      if (isset($_POST['username'])){
         $username = $_POST['username'];
         $pswd = $_POST['pswd'];

         $conn=pg_connect("host=localhost dbname=corporate user=root password=") or die(pg_last_error($conn));

         $query = "SELECT name FROM users WHERE username='$username' AND pswd='$pswd'";
         $result = pg_query($conn, $query);
         if (pg_num_rows($result) == 1){
            $_SESSION['name'] = pg_fetch_result($result,0,'name');
            $_SESSION['username'] = pg_fetch_result($result,0,'username');
            echo "You're logged in. Feel free to return at a later time.";
         }
      } else {
         include "login.html";
      }
   } else {
       $name = $_SESSION['name'];
       echo "Welcome back, $name!";
    }
?>
  
  








Related examples in the same category

1.Authentication Over HTTP
2.Basic authentication prompt
3.Checking the values returned from the authentication prompt
4.Get Users from users table
5.Enforcing Basic authentication
6.HTTP Authentication example
7.HTTP Authentication example forcing a new name/password
8.Hardcoding the username and password into a script
9.If user logged in
10.Only One Username and Password Is Valid
11.Usernames and Passwords Are Checked Against Data in a Database
12.Usernames and Passwords Are Checked Against Data in a File
13.Using HTTP authentication with a PHP script
14.The Username and Password Are Retrieved for Both Apache and IIS
15.Simple credentials checking:
16.User management with database