Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. <?php
  2.     //You will want to create a database connection class
  3.     //in the future instead of using variables
  4.     $dbhost = ''; //Database host
  5.     $dbuser = ''; //Database username
  6.     $dbpass = ''; //Database password
  7.     $dbname = ''; //Database name
  8.    
  9.     //Connect to MySQL
  10.     $con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
  11.     if (mysqli_connect_errno())
  12.     {
  13.         echo 'Cannot connect to the database:' mysqli_connect_error();
  14.     }
  15.    
  16.     //Check the user
  17.     if(isset($_POST['login']))
  18.     {
  19.         $user = mysqli_real_escape_string($con,$_POST['user']);
  20.         $pass = mysqli_real_escape_string($con,$_POST['pass']);
  21.         $run_user = mysqli_query($con, "SELECT * FROM `UserName` WHERE userName = '$user' AND pass = '$pass'");
  22.         $check_user = mysqli_num_rows($run_user);
  23.         if($check_user > 0)
  24.         {
  25.             $cookie_name = "user";
  26.             setcookie($cookie_name, $user, time() + (86400), "/"); //Set cookie for 1 day
  27.             $_SESSION['User'] = $user;
  28.             echo "Logging in as ".$user."...";
  29.             header("Location: /adminpanel.php"); //Use header instead of a javascript
  30.             exit;
  31.         }
  32.         else
  33.         {
  34.             echo "Error during login, check user/pass";
  35.         }
  36.     }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement