Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4. if(!isset($_SESSION['captcha'])) {
  5.     generateCaptcha();
  6. }
  7. if (isset($_SESSION['user']) AND isset($_SESSION['password']) and checkCaptcha()) {
  8.     redirect_to_main_page();
  9. }
  10.  
  11. if (isset($_POST['login'])) {
  12.     $_SESSION['user'] = $_POST['user'];
  13.     $_SESSION['password'] = $_POST['password'];
  14.  
  15.  
  16.     if(checkCaptcha())
  17.         redirect_to_main_page();
  18. }
  19.  
  20. function redirect_to_main_page()
  21. {
  22.     require_once('Database.php');
  23.  
  24. //    $connection = (new Database)->getConnection();
  25. //    $query = "SELECT * FROM exempledb.user WHERE user=? AND password =?";
  26. //    $stmt = $connection->prepare($query);
  27. //    if ($stmt === false) { trigger_error('Wrong SQL!', E_USER_ERROR); }
  28. //
  29. //    $stmt->bind_param('ss', $_SESSION['user'], $_SESSION['password']);
  30. //    $stmt->execute();
  31. //    $result = $stmt->get_result();
  32. //
  33. //    if (mysqli_num_rows($result) > 0) {
  34. //            header('Location: '."index.php");
  35. //
  36. //    }
  37.  
  38.  
  39.     //permite injection
  40.  
  41.     $connection = (new Database)->getConnection();
  42.     $user = $_SESSION["user"];
  43.     $password = $_SESSION["password"];
  44.     $query = "SELECT * FROM exempledb.user WHERE user='$user' and password='$password'";
  45.  
  46.     //in campul de password se pune:' OR 'a'='a
  47.     $result = $connection->query($query);
  48.  
  49.     if ($result) {
  50.         header('Location: ' . "index.php");
  51.         while ($rows = $result->fetch_assoc()) {
  52.             //ceva in caz ca vrem sa luam ceva din rezultat
  53.  
  54.         }
  55.     } else echo 'err';
  56. }
  57.  
  58.  
  59. function generateCaptcha()
  60. {
  61.          include("simple-php-captcha-master/simple-php-captcha.php");
  62.         try {
  63.             $_SESSION['captcha'] = simple_php_captcha();
  64.         } catch (Exception $e) {
  65.         }
  66. }
  67.  
  68. function checkCaptcha()
  69. {
  70.     if ($_POST['captcha'] == $_SESSION['captcha']['code']) {
  71.         return true;
  72.     }
  73.     generateCaptcha();
  74.     return false;
  75. }
  76.  
  77. ?>
  78.  
  79.  
  80. <html>
  81. <head>
  82.     <meta charset="UTF-8">
  83.     <title>Login</title>
  84.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
  85.           integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
  86. </head>
  87. <body>
  88. <div class="container">
  89.     <form action="login.php" method="post">
  90.         <label for="">Username<input type="text" name="user"></label><br>
  91.         <label for="">Password<input type="password" name="password"></label><br>
  92.         <?php echo '<img src="' . $_SESSION['captcha']['image_src'] . '"><br>'; ?>
  93.         <input type="text" name="captcha"><br>
  94.         <input type="submit" name="login" value="Login"><br>
  95.     </form>
  96. </div>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement