irwan

PHP - Simple User Login

Nov 12th, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. login_page.php
  2.  
  3. <form action="verify.php" method="post">
  4.     User Name:<br>
  5.     <input type="text" name="username"><br><br>
  6.     Password:<br>
  7.     <input type="password" name="password"><br><br>
  8.     <input type="submit" name="submit" value="Login">
  9. </form>
  10.  
  11.  
  12.  
  13.  
  14. verify.php
  15.  
  16. <?php
  17. if(isset($_POST['submit'])){
  18.     $dbHost = "localhost";        //Location Of Database usually its localhost
  19.     $dbUser = "";            //Database User Name
  20.     $dbPass = "";            //Database Password
  21.     $dbDatabase = "db_name";    //Database Name
  22.    
  23.     $db = mysql_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database.");
  24.     //Connect to the databasse
  25.     mysql_select_db($dbDatabase, $db)or die("Couldn't select the database.");
  26.     //Selects the database
  27.    
  28.     /*
  29.     The Above code can be in a different file, then you can place include'filename.php'; instead.
  30.     */
  31.    
  32.     //Lets search the databse for the user name and password
  33.     //Choose some sort of password encryption, I choose sha256
  34.     //Password function (Not In all versions of MySQL).
  35.     $usr = mysql_real_escape_string($_POST['username']);
  36.     $pas = hash('sha256', mysql_real_escape_string($_POST['password']));
  37.     $sql = mysql_query("SELECT * FROM users_table
  38.        WHERE username='$usr' AND
  39.        password='$pas'
  40.        LIMIT 1");
  41.     if(mysql_num_rows($sql) == 1){
  42.         $row = mysql_fetch_array($sql);
  43.         session_start();
  44.         $_SESSION['username'] = $row['username'];
  45.         $_SESSION['fname'] = $row['first_name'];
  46.         $_SESSION['lname'] = $row['last_name'];
  47.         $_SESSION['logged'] = TRUE;
  48.         header("Location: users_page.php"); // Modify to go to the page you would like
  49.         exit;
  50.     }else{
  51.         header("Location: login_page.php");
  52.         exit;
  53.     }
  54. }else{    //If the form button wasn't submitted go to the index page, or login page
  55.     header("Location: index.php");    
  56.     exit;
  57. }
  58. ?>
  59.  
  60.  
  61.  
  62. users_page.php
  63.  
  64. <?php
  65. session_start();
  66. if(!$_SESSION['logged']){
  67.     header("Location: login_page.php");
  68.     exit;
  69. }
  70. echo 'Welcome, '.$_SESSION['username'];
  71. ?>  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment