Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <h1>Login</h1>
  2. <form method="POST">
  3. <input type="text" name="username"><br />
  4. <input type="password" name="password"><br />
  5. <input type="submit">
  6. </form>
  7.  
  8. <?php
  9. session_start();
  10. if(isset($_POST['username'], $_POST['password'])){
  11. require 'core/connect.php';
  12.  
  13. $query = dbConnect()->prepare("SELECT username, password FROM users WHERE username=:username AND password=:password");
  14. $query->bindParam(':username', $_POST['username']);
  15. $query->bindParam(':password', $_POST['password']);
  16. $query->execute();
  17.  
  18. if($row = $query->fetch()){
  19. $_SESSION['username'] = $row['username'];
  20. header("Location: index.php");
  21. }
  22. }
  23. ?>
  24.  
  25. <h1>Register</h1>
  26. <form method="POST">
  27. <input type="text" name="username"><br />
  28. <input type="password" name="password"><br />
  29. <input type="submit">
  30. </form>
  31.  
  32. <?php
  33. session_start();
  34.  
  35. if(isset($_POST['username'], $_POST['password'])){
  36. require 'core/connect.php';
  37.  
  38. $query = dbConnect()->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
  39. $query->bindParam(':username', $_POST['username']);
  40. $query->bindParam(':password', $_POST['password']);
  41.  
  42. if($query->execute()){
  43. header("Location: index.php");
  44. } else{
  45. echo 'ERROR';
  46. }
  47. }
  48. ?>
  49.  
  50. <?php
  51. session_start();
  52.  
  53. if(isset($_SESSION['username'])){
  54. echo 'Welcome!', '<a href="logout.php">Logout</a>';
  55. } else {
  56. echo '<a href="login.php">Login</a><br />
  57. <a href="register.php">Register</a>';
  58. }
  59. ?>
  60.  
  61. <?php
  62. function dbConnect(){
  63. try{
  64. $username = 'root';
  65. $password = '';
  66. $conn = new pdo("mysql:host=localhost;dbname=test;", $username, $password);
  67. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  68. return $conn;
  69.  
  70. } catch(PDOException $e){
  71. echo 'ERROR', $e->getMessage();
  72. }
  73. }
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement