- function.php///////////
- <?php
- session_start();
- $conn = mysqli_connect("localhost", "root", "", "loginregister");
- // IF
- if(isset($_POST["action"])){
- if($_POST["action"] == "register"){
- register();
- }
- else if($_POST["action"] == "login"){
- login();
- }
- }
- // REGISTER
- function register(){
- global $conn;
- $name = $_POST["name"];
- $username = $_POST["username"];
- $password = $_POST["password"];
- if(empty($name) || empty($username) || empty($password)){
- echo "Please Fill Out The Form!";
- exit;
- }
- $user = mysqli_query($conn, "SELECT * FROM tb_user WHERE username = '$username'");
- if(mysqli_num_rows($user) > 0){
- echo "Username Has Already Taken";
- exit;
- }
- $query = "INSERT INTO tb_user VALUES('', '$name', '$username', '$password')";
- mysqli_query($conn, $query);
- echo "Registration Successful";
- }
- // LOGIN
- function login(){
- global $conn;
- $username = $_POST["username"];
- $password = $_POST["password"];
- $user = mysqli_query($conn, "SELECT * FROM tb_user WHERE username = '$username'");
- if(mysqli_num_rows($user) > 0){
- $row = mysqli_fetch_assoc($user);
- if($password == $row['password']){
- echo "Login Successful";
- $_SESSION["login"] = true;
- $_SESSION["id"] = $row["id"];
- }
- else{
- echo "Wrong Password";
- exit;
- }
- }
- else{
- echo "User Not Registered";
- exit;
- }
- }
- ?>
- index.php/////////
- <?php
- require 'function.php';
- if(isset($_SESSION["id"])){
- $id = $_SESSION["id"];
- $user = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM tb_user WHERE id = $id"));
- }
- else{
- header("Location: login.php");
- }
- ?>
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>Index</title>
- </head>
- <body>
- <h1>Welcome <?php echo $user["name"]; ?></h1>
- <a href="logout.php">Logout</a>
- </body>
- </html>
- login.php/////////////
- <?php
- require 'function.php';
- if(isset($_SESSION["id"])){
- header("Location: index.php");
- }
- ?>
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>Login</title>
- </head>
- <body>
- <h2>Login</h2>
- <form autocomplete="off" acti method="post">
- <input type="hidden" id="action" value="login">
- <label for="">Username</label>
- <input type="text" id="username" value=""> <br>
- <label for="">Password</label>
- <input type="password" id="password" value=""> <br>
- <button type="button" >Login</button>
- </form>
- <br>
- <a href="register.php">Go To Register</a>
- <?php require 'script.php'; ?>
- </body>
- </html>
- logout.php///////
- <?php
- require 'function.php';
- $_SESSION = [];
- session_unset();
- session_destroy();
- header("Location: login.php");
- ?>
- register.php/////
- <?php
- require 'function.php';
- if(isset($_SESSION["id"])){
- header("Location: index.php");
- }
- ?>
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>Register</title>
- </head>
- <body>
- <h2>Register</h2>
- <form autocomplete="off" acti method="post">
- <input type="hidden" id="action" value="register">
- <label for="">Name</label>
- <input type="text" id="name" value=""> <br>
- <label for="">Username</label>
- <input type="text" id="username" value=""> <br>
- <label for="">Password</label>
- <input type="password" id="password" value=""> <br>
- <button type="button" >Register</button>
- </form>
- <br>
- <a href="login.php">Go To Login</a>
- <?php require 'script.php'; ?>
- </body>
- </html>
- script.php
- [removed]
- [removed]
- [removed]
- function submitData(){
- $(document).ready(function(){
- var data = {
- name: $("#name").val(),
- username: $("#username").val(),
- password: $("#password").val(),
- action: $("#action").val(),
- };
- $.ajax({
- url: 'function.php',
- type: 'post',
- data: data,
- success:function(response){
- alert(response);
- if(response == "Login Successful"){
- [removed].reload();
- }
- }
- });
- });
- }
- [removed]