Guest User

PhP code

a guest
Feb 26th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2. $db_username = "peoplein"; //Input your database username here
  3. $db_password = "xxxxxxx"; //Input your database password here
  4. $db_host = "localhost"; //Input your database host here
  5. $db_name = "xxxxxxxx"; //Input your database name here
  6.  
  7. $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
  8.  
  9. try {
  10.     $connection = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_username, $db_password, $options);
  11.     $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. }
  13.  
  14. catch(PDOException $ex) {
  15.     echo "Cannot connect to database.";
  16. }
  17.  
  18. header('Content-Type: text/html; charset=utf-8');
  19. ?>
  20.  
  21. <!doctype html>
  22. <html>
  23. <head>
  24. <title>Login</title>
  25. </head>
  26. <body>
  27.  
  28. <center>
  29. <h3>Login</h3>
  30. <form action="" method="POST">
  31.     Username: <input type="text" name="user">
  32.     Password: <input type="password" name="pass">
  33.     <input type="submit" value="Login" name="submit" />
  34. </form>
  35.  
  36.  
  37. <?php
  38.  
  39. //Grabs the database connection
  40. require("path to database connection file");
  41.  
  42. $user = $_POST["user"];
  43. $pass = $_POST["pass"];
  44.  
  45. $query="SELECT * FROM login WHERE username=:username AND password=:password";
  46.  
  47. $params->execute(array(':username' => $user,
  48.                        ':password' => $pass));
  49.  
  50. try{
  51.     $stmt = $connection->prepare($query);
  52.     $result = $stmt->execute($params);
  53. }
  54.  
  55. catch(PDOException $ex){
  56.     echo ("Failed to run query: " . $ex->getMessage());
  57. }
  58.  
  59. $fetch = $stmt->fetch();
  60.  
  61. if($fetch) {
  62.     while($row=mysql_fetch_assoc($query)){
  63.         $usernamefetch=$fetch['username'];
  64.         $passwordfetch=$fetch['password'];
  65.     }
  66.  
  67.     if($user == $usernamefetch && $pass == $passwordfetch){
  68.         session_start();
  69.         $_SESSION['sess_user']=$user;
  70.        
  71.         }else{
  72.        
  73.         echo "Wrong data";
  74.         }
  75.         }
  76. ?>
  77.         <meta http-equiv='refresh' content="0" url="http://peopleinvestment.ro/filip/admin.php">
  78.  
  79. </body>
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment