Advertisement
Guest User

everythingishere

a guest
Dec 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. login.php
  2. <?php
  3. session_start();
  4.   if(isset($_POST)){
  5.     //make connection
  6.     $servername = "localhost";
  7.     $username = "root";
  8.     $password = "";
  9.     $dbname = "exam";
  10.  
  11.     // Create connection
  12.     $conn = new mysqli($servername, $username, $password, $dbname);
  13.     // Check connection
  14.     if ($conn->connect_error) {
  15.         die("Connection failed: " . $conn->connect_error);
  16.     }
  17.     if((isset($_POST['username']) && !empty($_POST['username'])) && (isset($_POST['password']) && !empty($_POST['password']))){
  18.     $username = trim(htmlspecialchars($_POST['username']));
  19.     $password = md5(trim($_POST['password']));
  20.     $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
  21.     $row = $conn->query($query);
  22.     if($fetch = $row->num_rows == 1){
  23.       $guka = $row->fetch_assoc();
  24.  
  25.       $_SESSION['username'] =  $guka['username'];
  26.       $_SESSION['password'] =  $guka['password'];
  27.  
  28.       print_r($_SESSION);
  29.     }else{
  30.       echo "incorrect password";
  31.     }
  32.  
  33.   }
  34. $conn->close();
  35.   }
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="en" dir="ltr">
  39.   <head>
  40.     <meta charset="utf-8">
  41.     <title></title>
  42.   </head>
  43.   <body>
  44.     <form class="" method="post">
  45.       <input type="text" name="username" placeholder="username">
  46.       <input type="password" name="password" placeholder="password">
  47.       <input type="submit" name="" value="submit">
  48.     </form>
  49.     <?php
  50.     if(isset($_SESSION)){
  51.  
  52.       echo $_SESSION['username'];
  53.     }
  54.  
  55.      ?>
  56.   </body>
  57. </html>
  58.  
  59. index.php
  60.  
  61. <?php
  62. if(isset($_SESSION)){
  63. header("Location: login.php");
  64. }
  65.  
  66.  ?>
  67.  
  68. register.php
  69.  
  70. <!DOCTYPE html>
  71. <html lang="en" dir="ltr">
  72.   <head>
  73.     <meta charset="utf-8">
  74.     <title></title>
  75.   </head>
  76.   <body>
  77.     <form class=""  action="check.php" method="post">
  78.       <input type="text" name="name" placeholder="name"><br>
  79.       <input type="text" name="username" placeholder="username"><br>
  80.       <input type="password" name="password" placeholder="password">
  81.       <input type="submit" name="submit" value="submit">
  82.     </form>
  83.   </body>
  84. </html>
  85.  
  86.  
  87.  
  88. check.php
  89.  
  90. <?php
  91. //make connection
  92. $servername = "localhost";
  93. $username = "root";
  94. $password = "";
  95. $dbname = "exam";
  96.  
  97. // Create connection
  98. $conn = new mysqli($servername, $username, $password, $dbname);
  99. // Check connection
  100. if ($conn->connect_error) {
  101.     die("Connection failed: " . $conn->connect_error);
  102. }
  103.  
  104. if (isset($_POST)) {
  105.   if($_POST['username'] === null || empty($_POST['username']) || strlen($_POST['username']) < 4){
  106.     echo "error incorrect username";
  107.     die();
  108.   }
  109.  
  110.   if($_POST['password'] === null || empty($_POST['password']) || strlen($_POST['password']) < 3){
  111.     echo "error incorrect password";
  112.     die();
  113.   }
  114.  
  115.   if($_POST['name'] === null || empty($_POST['name'])){
  116.     echo "error incorrect name";
  117.     die();
  118.   }
  119.  
  120.    $username = trim(htmlspecialchars($_POST['username']));
  121.    $name = trim(htmlspecialchars($_POST['name']));
  122.    $password = md5(trim($_POST['password']));
  123.    $ip = $_SERVER['REMOTE_ADDR'];
  124.    $sql = "INSERT INTO users (username,name,password,ip) VALUES ('$username','$name','$password','$ip')";
  125.  
  126.   if ($conn->query($sql) === TRUE) {
  127.       echo "New record created successfully";
  128.   } else {
  129.       echo "Error: " . $sql . "<br>" . $conn->error;
  130.   }
  131.   $conn->close();
  132.  
  133.   header("Location: login.php");
  134. }
  135.  
  136.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement