Register and Login With​ Using PHP 7

Register and Login With​ Using PHP 7


Register and Login With​ Using PHP 7




https://drive.google.com/file/d/1nbPi3ibXY47kAYQqIBTDzWHPb8jOQUcs/view?usp=sharing
  • 1. connection DB

  • 2. error.php

  • 3. Login.php

  • 4. logout.php

  • 5. Register.php

  • 6. serve.php

  • 7. dashboard.php

MySQL database

CREATE TABLE `user_login` ( `id` int(6) NOT NULL, `username` varchar(40) NOT NULL, `email` varchar(50) NOT NULL, `phone_number` int(10) NOT NULL, `password` varchar(40) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1. connection DB


<?php
    $servername     =   'localhost';
    $username       =   'root';
    $password       =   '';
    $dbname         =   "db_login";
    $connection     =   mysqli_connect($servername, $username,
$password,"$dbname");
if(!$connection)
{
   die('Could not Connect My Sql:' .mysql_error());

}



2. error.php


<?php
// error foreach
if (count($errors) > 0) :
?>
<div class="error">

<?php foreach ($errors as $error) : ?>
<p><?php echo $error ?></p>

<?php endforeach ?>
</div>

<?php  endif ?>

3. Login.php



<?php
// include file server.php
include('server.php');
include_once 'server.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login</title>

     <!-- Library -->
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/additional-methods.min.js"></script>


    <!-- Font Icon -->
    <link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic-font.min.css">

    <!-- Main css -->
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

    <Style>
        .container, .form-submit{
            border-radius: 1px;
            padding: 7px 23px;
        }
        .main {
            background: #f8f8f8;
            padding: 50px 0;
        }
    </Style>

    <div class="main">
        <!-- Sing in  Form -->
        <section class="sign-in">
            <div class="container">
                <div class="signin-content">
                    <div class="signin-image">
                        <figure><img src="images/signin-image.jpg" alt="sing up image"></figure>
                        <a href="Register.php" class="signup-image-link">Create your free account</a>
                    </div>

                    <div class="signin-form">
                        <h2 class="form-title">Log in</h2>
                        <form action="Login.php" method="POST" class="register-form" id="login-form">
                        <?php include('errors.php'); ?>
                        <!-- Email -->
                            <div class="form-group">
                                <label for="email"><i class="zmdi zmdi-email"></i></label>
                                <input type="email" name="email" id="email" placeholder="Your email"/>
                            </div>
                            <div class="form-group">
                                <label for="your_pass"><i class="zmdi zmdi-lock"></i></label>
                                <input type="password" name="password" id="password" placeholder="Password"/>
                            </div>
                            <div class="form-group">
                                <input type="checkbox" name="remember-me" id="remember-me" class="agree-term"/>
                                <label for="remember-me" class="label-agree-term"><span><span></span></span>Remember me</label>
                            </div>
                            <div class="form-group form-button">
                                <input type="submit" name="login_user" id="login_user" class="form-submit" value="Log in"/>
                            </div>
                        </form>
                        <div class="social-login">
                            <span class="social-label">Or login with</span>
                            <ul class="socials">
                                <li><a href="https://www.facebook.com/soengsouy.com.kh/?ref=bookmarks"><i class="display-flex-center zmdi zmdi-facebook"></i></a></li>
                                <li><a href="https://twitter.com/SoengSouy1"><i class="display-flex-center zmdi zmdi-twitter"></i></a></li>
                                <li><a href="#"><i class="display-flex-center zmdi zmdi-google"></i></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- JS -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="js/main.js"></script>
</body>

</html>


4. logout.php


<?php
// Initialize the session
session_start();

// Unset all of the session variables
$_SESSION = array();

// Destroy the session.
session_destroy();

// Redirect to login page
header("location: Login.php");
exit;

?>

5. Register.php


<?php
    // include file errors
    include('server.php'); 
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Sign Up Form by Colorlib</title>

    <!-- Font Icon -->
    <link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic-font.min.css">

    <!-- Main css -->
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

    <Style>
        .container, .form-submit{
            border-radius: 1px;
            padding: 7px 23px;
        }
        .main {
            background: #f8f8f8;
            padding: 50px 0;
        }
        .alert-danger {
            color:red;
        }
    </Style>
    <div class="main">
        <!-- Sign up form -->
        <section class="signup">
            <div class="container">
                <div class="signup-content">
                    <div class="signup-form">
                        <h2 class="text-center">Register</h2>
                        <p>Create your free account.</p>
                        <br>
                        <form action="Register.php" method="POST" class="register-form" id="register-form">
                        <?php
                        // include file errors
                        include('errors.php'); 
                        ?>
                            <div class="form-group">
                                <label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
                                <input type="text" name="username" id="username" placeholder="Your Name" />
                            </div>
                            <div class="form-group">
                                <label for="email"><i class="zmdi zmdi-email"></i></label>
                                <input type="email" name="email" id="email" placeholder="Your Email"/>
                            </div>
                            <div class="form-group">
                                <label for="tel"><i class="zmdi zmdi-smartphone-iphone"></i></label>
                                <input type="tel" name="phone_number" id="phone_number" placeholder="Your Phone number"/>
                            </div>
                            <div class="form-group">
                                <label for="pass"><i class="zmdi zmdi-lock"></i></label>
                                <input type="password" name="password_1" id="password_1" placeholder="Password"/>
                            </div>
                            <div class="form-group">
                                <label for="re-pass"><i class="zmdi zmdi-lock-outline"></i></label>
                                <input type="password" name="password_2" id="password_2" placeholder="Repeat your password"/>
                            </div>
                            <div class="form-group">
                                <input type="checkbox" name="agree-term" id="agree-term" class="agree-term" />
                                <label for="agree-term" class="label-agree-term"><span><span></span></span>I agree all statements in  <a href="#" class="term-service">Terms of service</a></label>
                            </div>
                            <div class="form-group form-button">
                                <input type="submit" name="reg_user" id="reg_user" class="form-submit" value="Register"/>
                            </div>
                        </form>
                    </div>
                    <div class="signup-image">
                        <figure><img src="images/signup-image.jpg" alt="sing up image"></figure>
                        <p>Already have an Arena account?</p>
                        <a href="Login.php" class="signup-image-link">Login</a>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- JS -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="js/main.js"></script>
</body><!-- This templates was made by Colorlib (https://colorlib.com) -->

</html>



6. serve.php


<?php
include_once 'database.php';
// start session
session_start();

// initializing variables

$username   = "";
$email      = "";
$errors     = array();

// connect to the database
include_once 'database.php';
$register = '';
// isset reg_user
if (isset($_POST['reg_user']))
{
// receive all input values from the form
$username = mysqli_real_escape_string($connection, $_POST['username']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone_number = mysqli_real_escape_string($connection, $_POST['phone_number']);
$password_1 = mysqli_real_escape_string($connection, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($connection, $_POST['password_2']);

// empty form register

if (empty($username)) 
{
array_push($errors, '<div class="alert-danger" role="alert">Name is required</div>');
}

if (empty($email)) 
{
array_push($errors,'<div class="alert-danger" role="alert">Email is required</div>');
}

if (empty($phone_number))
{
array_push($errors, '<div class="alert-danger" role="alert">Phone number is required</div>');
}

if (empty($password_1))
{
array_push($errors, '<div class="alert-danger" role="alert">Password is required</div>');
}
if ($password_1 != $password_2)
{
array_push($errors, '<div class="alert-danger" role="alert">The two passwords do not match</div>');
}

// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM user_login WHERE username ='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($connection, $user_check_query);
$user = mysqli_fetch_assoc($result);

if ($user)
{ // if user exists
if ($user['username'] === $username) 
{
array_push($errors, '<div class="alert-danger" role="alert">Username already exists</div>');
}

if ($user['email'] === $email)
{
array_push($errors, '<div class="alert-danger" role="alert">Email already exists</div>');
}
}

// Finally, register user if there are no errors in the form
if (count($errors) == 0)
{
$password = md5($password_1);//encrypt the password before saving in the database

// insert file register
$query = "INSERT INTO `user_login`(`username`,`email`, `phone_number`, `password`)
VALUES('$username', '$email','$phone_number', '$password')";
mysqli_query($connection, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: dashboard.php');
$success = "Signup Success ! Please Login to Continue.";
}
}

//**********************

// login user
if (isset($_POST['login_user'])) {
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);

// empty file email and password
if (empty($email)) {
array_push($errors, '<div class="alert-danger" role="alert">email is required</div>');
}
if (empty($password)) {
array_push($errors, '<div class="alert-danger" role="alert">password is required</div>');
}

if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM user_login WHERE email='$email' and password='$password'";
$results = mysqli_query($connection, $query);

if (mysqli_num_rows($results) == 1) {
$_SESSION['email'] = $email;
$_SESSION['success'] = '<h4 class="text-center">You are now logged in<h4>';
header('location: dashboard.php');
}else {
array_push($errors,'<div class="alert-danger" role="alert">Wrong email/password, Please login again.</div>');
}
}
}

?>



7. dashboard.php

<?php session_start(); if (!isset($_SESSION['email'])) { $_SESSION['msg'] = "You must log in first"; header('location: Login.php'); } if (isset($_GET['logout'])) { session_destroy(); unset($_SESSION['email']); header("location:Login.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>My Dashoard</title> <!-- Font Icon --> <link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic-font.min.css"> <!-- Main css --> <link rel="stylesheet" href="css/style.css"> </head> <body> <Style> .container, .form-submit{ border-radius: 1px; padding: 7px 23px; text-align: center; } .main { background: #f8f8f8; padding: 50px 0; } </Style> <div class="main"> <div class="container"> <h1 class="text-center">My Dashoard</h1> <!-- logged in user information --> <?php if (isset($_SESSION['email'])) : ?> <p>Welcome <strong><?php echo $_SESSION['email']; ?></strong></p> <?php endif ?> <br> <a href="change_password.php" class="signup-image-link">Change Password</a> <br> <hr> <a href="logout.php" class="signup-image-link">Logout</a> <!-- notification message --> <?php if (isset($_SESSION['success'])) : ?> <div class="error success" > <h3> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?> </h3> </div> <?php endif ?> </div> </div> <!-- JS --> <script src="vendor/jquery/jquery.min.js"></script> <script src="js/main.js"></script> </body> </html>

Reactions

Post a Comment

0 Comments

close