Facebook
From andja, 9 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 441
  1. function.php///////////
  2.  
  3.  
  4. <?php
  5. session_start();
  6. $conn = mysqli_connect("localhost", "root", "", "loginregister");
  7.  
  8. // IF
  9. if(isset($_POST["action"])){
  10.   if($_POST["action"] == "register"){
  11.     register();
  12.   }
  13.   else if($_POST["action"] == "login"){
  14.     login();
  15.   }
  16. }
  17.  
  18. // REGISTER
  19. function register(){
  20.   global $conn;
  21.  
  22.   $name = $_POST["name"];
  23.   $username = $_POST["username"];
  24.   $password = $_POST["password"];
  25.  
  26.   if(empty($name) || empty($username) || empty($password)){
  27.     echo "Please Fill Out The Form!";
  28.     exit;
  29.   }
  30.  
  31.   $user = mysqli_query($conn, "SELECT * FROM tb_user WHERE username = '$username'");
  32.   if(mysqli_num_rows($user) > 0){
  33.     echo "Username Has Already Taken";
  34.     exit;
  35.   }
  36.  
  37.   $query = "INSERT INTO tb_user VALUES('', '$name', '$username', '$password')";
  38.   mysqli_query($conn, $query);
  39.   echo "Registration Successful";
  40. }
  41.  
  42. // LOGIN
  43. function login(){
  44.   global $conn;
  45.  
  46.   $username = $_POST["username"];
  47.   $password = $_POST["password"];
  48.  
  49.   $user = mysqli_query($conn, "SELECT * FROM tb_user WHERE username = '$username'");
  50.  
  51.   if(mysqli_num_rows($user) > 0){
  52.  
  53.     $row = mysqli_fetch_assoc($user);
  54.  
  55.     if($password == $row['password']){
  56.       echo "Login Successful";
  57.       $_SESSION["login"] = true;
  58.       $_SESSION["id"] = $row["id"];
  59.     }
  60.     else{
  61.       echo "Wrong Password";
  62.       exit;
  63.     }
  64.   }
  65.   else{
  66.     echo "User Not Registered";
  67.     exit;
  68.   }
  69. }
  70. ?>
  71.  
  72. index.php/////////
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. <?php
  80. require 'function.php';
  81. if(isset($_SESSION["id"])){
  82.   $id = $_SESSION["id"];
  83.   $user = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM tb_user WHERE id = $id"));
  84. }
  85. else{
  86.   header("Location: login.php");
  87. }
  88. ?>
  89. <!DOCTYPE html>
  90. &lt;html lang="en" dir="ltr"&gt;
  91.   &lt;head&gt;
  92.     &lt;meta charset="utf-8"&gt;
  93.     &lt;title&gt;Index&lt;/title&gt;
  94.   &lt;/head&gt;
  95.   &lt;body&gt;
  96.     <h1>Welcome &lt;?php echo $user["name"]; ?&gt;</h1>
  97.     <a href="logout.php">Logout</a>
  98.   &lt;/body&gt;
  99. &lt;/html&gt;
  100.  
  101.  
  102. login.php/////////////
  103.  
  104.  
  105. &lt;?php
  106. require 'function.php';
  107. if(isset($_SESSION["id"])){
  108.   header("Location: index.php");
  109. }
  110. ?&gt;
  111. <!DOCTYPE html>
  112. &lt;html lang="en" dir="ltr"&gt;
  113.   &lt;head&gt;
  114.     &lt;meta charset="utf-8"&gt;
  115.     &lt;title&gt;Login&lt;/title&gt;
  116.   &lt;/head&gt;
  117.   &lt;body&gt;
  118.     <h2>Login</h2>
  119.     &lt;form autocomplete="off" acti method="post"&gt;
  120.       &lt;input type="hidden" id="action" value="login"&gt;
  121.       <label for="">Username</label>
  122.       &lt;input type="text" id="username" value=""&gt; <br>
  123.       <label for="">Password</label>
  124.       &lt;input type="password" id="password" value=""&gt; <br>
  125.       <button type="button" >Login</button>
  126.     &lt;/form&gt;
  127.     <br>
  128.     <a href="register.php">Go To Register</a>
  129.     &lt;?php require 'script.php'; ?&gt;
  130.   &lt;/body&gt;
  131. &lt;/html&gt;
  132.  
  133.  
  134. logout.php///////
  135.  
  136.  
  137.  
  138.  
  139. &lt;?php
  140. require 'function.php';
  141. $_SESSION = [];
  142. session_unset();
  143. session_destroy();
  144. header("Location: login.php");
  145. ?&gt;
  146.  
  147.  
  148. register.php/////
  149.  
  150.  
  151.  
  152.  
  153.  
  154. &lt;?php
  155. require 'function.php';
  156. if(isset($_SESSION["id"])){
  157.   header("Location: index.php");
  158. }
  159. ?&gt;
  160. <!DOCTYPE html>
  161. &lt;html lang="en" dir="ltr"&gt;
  162.   &lt;head&gt;
  163.     &lt;meta charset="utf-8"&gt;
  164.     &lt;title&gt;Register&lt;/title&gt;
  165.   &lt;/head&gt;
  166.   &lt;body&gt;
  167.     <h2>Register</h2>
  168.     &lt;form autocomplete="off" acti method="post"&gt;
  169.       &lt;input type="hidden" id="action" value="register"&gt;
  170.       <label for="">Name</label>
  171.       &lt;input type="text" id="name" value=""&gt; <br>
  172.       <label for="">Username</label>
  173.       &lt;input type="text" id="username" value=""&gt; <br>
  174.       <label for="">Password</label>
  175.       &lt;input type="password" id="password" value=""&gt; <br>
  176.       <button type="button" >Register</button>
  177.     &lt;/form&gt;
  178.     <br>
  179.     <a href="login.php">Go To Login</a>
  180.     &lt;?php require 'script.php'; ?&gt;
  181.   &lt;/body&gt;
  182. &lt;/html&gt;
  183.  
  184.  
  185. script.php
  186.  
  187.  
  188.  
  189. [removed]
  190. [removed]
  191. [removed]
  192.   function submitData(){
  193.     $(document).ready(function(){
  194.       var data = {
  195.         name: $("#name").val(),
  196.         username: $("#username").val(),
  197.         password: $("#password").val(),
  198.         action: $("#action").val(),
  199.       };
  200.  
  201.       $.ajax({
  202.         url: 'function.php',
  203.         type: 'post',
  204.         data: data,
  205.         success:function(response){
  206.           alert&#40;response&#41;;
  207.           if(response == "Login Successful"){
  208.             [removed].reload();
  209.           }
  210.         }
  211.       });
  212.     });
  213.   }
  214. [removed]
  215.  
  216.  
  217.