Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. <?php
  2. //if the form was submit
  3. if(isset($_POST['submit'] && !empty($_POST['username']))){
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6.  
  7. try{//connection to database(some database)
  8. $dbh = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",$username,$password,$options);
  9. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  11.  
  12. //check if the username exists and the password is the same as in the database
  13. $sql="SELECT * FROM users WHERE username=$username AND password=$password";//match username and password
  14. $result = $dbh->prepare($sql);
  15. $result->execute();
  16. if($result){
  17. return;// (what ever we want to make the connection)
  18. //we can set cookie/session or what ever we wish to make the username logged.
  19. }
  20. return false;//username or password not match, no connection
  21.  
  22. }
  23. catch(PDOException $e){
  24. throw new Exception($e->getMessage());
  25. }
  26. }
  27. }
  28.  
  29. ?>
  30.  
  31. <!doctype html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="utf-8">
  35. <title>Login</title>
  36. </head>
  37.  
  38. <body>
  39. <form action="" method="POST">
  40. <label>Username:</label><input type="text" name="username" /><br/>
  41. <label>Password:</label><input type="password" name="password" /><br/>
  42. <input type="submit" name="submit" value="Connect" />
  43. </form>
  44. </body>
  45.  
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement