Gradient Able bootstrap admin template by codedthemes

Create Admin Login and Logout Page

In this post, I am going to let you know how to create a Normal/Admin register and login using PHP and MySQL. I also provide features to logout from admin dashboard. I will use PHP session to maintain user visibility.

The Login system of the user helps to restrict the access of sensitive information or pages from unauthorized user.

The admin pages in the application are not accessible to normal users. To log in, all users (administrators and normal users) utilise the same form. Normal users are redirected to the index page after checking in, whereas admin users are redirected to the admin pages.

I am using SB Admin 2 theme.It’s a open source bootstrap based theme.

Registration and Login form in PHP and MySQL

Let’s build a user management system with two types of users: admins and regular users. I’ll save everything to a MySQL database.

Читайте также:  Java array is not iterable

Create the following files and directories in the xampp/htdocs(www if you’re using wamp) folder for a user-management application:

  • index.php: This is the normal user’s home page.
  • account.php: This file will contain user registration and login methods.
  • admin/dashboard.php: This file will be used to display the admin user’s welcome dashboard page..
  • header.php: This file contains information about the header theme.
  • sidebar.php: This is a partial file that will display an html view of the ace admin sidebar.
  • login.php: It will have a user sign-in view.
  • register.php: This file provides user registration information; I use the same registration form for admins and regular users.

Create MySQL Connection Using PHP

We’ll start by creating a database called test. Also, using the SQL script below, create a login name table in this database:

CREATE TABLE `login` ( `id` int(11) NOT NULL, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `user_type` enum('normal','admin','') NOT NULL DEFAULT 'normal', `status` enum('active','pending','deleted','') NOT NULL DEFAULT 'pending', `authtoken` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Create Connection Using PHP and MySQL

Let’s use PHP to make a connection with MySQL data. The following code was added to the top of the account.php file.

// connect to database $db = mysqli_connect('localhost', 'root', '', 'test');

Create Partial header.php

We’ll make a header.php file and paste the code below into it. This file will contain all of the CSS and JS include paths. I also used the session start method at the top of the file to start a PHP session.

Add a link to the css/js files directly under the file’s head section.

Create Partial sidebar.php

We’ll make a sidebar.php file and paste the code below into it. This file has a sidebar menu option, which you can customize for both admins and regular users.

Created Admin and User Registration

Using SB admin2 Theme Theme, we’ll first setup user and admin registration. Now, open register.php in your preferred text editor and begin writing some code.

User and Admin Signup HTML

Create an HTML form that receives user input and saves it to the mysql login table. I’ve added the following code to this file:

     
Create an Account!

">
Normal

Forgot Password?
Already have an account? Login!
Copyright © Your Website 2020

At the top of the file, I’ve imported header.php and account.php. Create a POST type form request to submit user inputs to the server-side register() procedure after the user presses the submit button.

Added Method to save User Form Data

Let’s add a method to the account.php file that validates and saves data.

// variable declaration $fname = ""; $lname = ""; $email = ""; $errors = array(); // submit clicked if (isset($_POST['submit_btn'])) < signup(); >// escape string function parse_input($val) < global $db; return mysqli_real_escape_string($db, trim($val)); >// REGISTER USER function signup() < // Global variables global $db, $errors, $fname, $lname, $email; // receive all input values from the form. $fname = parse_input($_POST['fname']); $lname = parse_input($_POST['lname']); $email = parse_input($_POST['email']); $pass = parse_input($_POST['pass']); $c_pass = parse_input($_POST['c_pass']); $user_type = parse_input($_POST['user_type']); // form validation if (empty($fname)) < array_push($errors, "first name is required"); >if (empty($lname)) < array_push($errors, "last name is required"); >if (empty($email)) < array_push($errors, "Email is required"); >if (empty($pass)) < array_push($errors, "Password is required"); >if ($pass != $c_pass) < array_push($errors, "The both passwords do not match"); >// register user if there are no errors in the form if (count($errors) == 0) < //encrypt the password $password = md5($pass); $query = "INSERT INTO login (first_name, last_name, email, user_type, password) VALUES('$fname', '$lname', '$email', '$user_type', '$password')"; mysqli_query($db, $query); // get id of the created user $logged_in_user_id = mysqli_insert_id($db); $_SESSION['user_info'] = getUserById($logged_in_user_id); $_SESSION['msg'] = "New user successfully created!!"; if (isset($_POST['user_type'])) < header('location: admin/dashboard.php'); >else < header('location: index.php'); >> > function display_error() < global $errors; if (count($errors) >0)< echo '
'; foreach ($errors as $error)< echo $error .'
'; > echo '
'; > >

I’ve created three methods in the code above:

parse input– The mysqli real_escape_string() method is used to parse user inputs.
signup– This method is used to process and save all form-posted payloads into the MySQL login table.
display error– This technique separates all errors and bids them with a div element, which is then printed on the user registration page.

Created Login/Sign-In Using MySQL

I’ve already established a user registration feature; now I’ll add a user logged-in feature. I’ll take the user’s email address and password and transmit it to the login method. We’ll redirect you to the user page (normal user) or the admin dashboard page once the user credential has been validated (for admin users).

User and Admin Login HTML

Create an HTML form to collect user input and save it to the MySQL login table. I’ve added the following code to this file:

     
Welcome Back!
Remember Me

Forgot Password?
Create an Account!

I’ve added an account and header file at the top of the file, then generated a form with username and pass input fields. The login() method receives the form credentials.

Added Method to Login User

In the account.php file, we will add a login mechanism. This method is in charge of validating and establishing a session with the user.

// login() function if (isset($_POST['login_btn'])) < login(); >// LOGIN USER function login() < global $db, $email, $errors; // grap form values $email = parse_input($_POST['email']); $password = parse_input($_POST['password']); // make sure form is filled properly if (empty($email)) < array_push($errors, "email is required"); >if (empty($password)) < array_push($errors, "Password is required"); >// attempt login if no errors on form if (count($errors) == 0) < $password = md5($password); $query = "SELECT * FROM login WHERE email='$email' AND password='$password' LIMIT 1"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) < // user found // check if user is admin or user $logged_in_user = mysqli_fetch_assoc($results); if ($logged_in_user['user_type'] == 'admin') < $_SESSION['user_info'] = $logged_in_user; header('location: admin/dashboard.php'); >else < $_SESSION['user_info'] = $logged_in_user; header('location: index.php'); >>else < array_push($errors, "Wrong username/password entered"); >> > // return user info based on ID function getUserById($id) < global $db; $query = "SELECT * FROM login WHERE . $id; $result = mysqli_query($db, $query); $user = mysqli_fetch_assoc($result); return $user; >function isLoggedIn() < if (isset($_SESSION['user_info'])) < return true; >else < return false; >>

We’ve verified the user’s credentials, and if everything checks up, we’ll redirect based on the user’s type.

The getUserById() method is used to retrieve information about a user based on their id. It returns user details, which are stored in the session variable.

The isLoggedIn() method is used to determine whether a user is logged in or not. If the session contains a user info variable with a value set, the user is logged in; return True otherwise, False.

Logout Using PHP

We’ll implement a logout feature. The following code has been added to the account.php file.

How To Check Logged-in User is Admin

As you are aware, I have saved user information in the PHP session user info variable, which we will use to verify whether the user is an administrator or not. Add the following code to the account.php file.

dashboard- normal-user

Conclusion

I hope you found this tutorial useful. We learned how to utilise PHP and MySQL to develop a user management system. For a user/admin, you can add further functionality such as listing and editing capabilities. You can adapt it to your requirements and use it in your projects. If you have any questions or concerns about this tutorial, please post them in the comments section below.

2 thoughts on “ Create Admin Login and Logout Page ”

how can download all file

Источник

How to Use the HTML Admin template in PHP?

Admin templates are a set of web pages, built with HTML, CSS, and JavaScript or any JavaScript libraries used to create the user interface of the backend of a web application. These pre-built pages are integrated with the web application for doing backend tasks like maintaining the website, user and content management, installation and configuration of website software, and tracking data like network traffic and user visits to the website, to improve the performance of the website. Admin template makes your work easy it saves developer time and effort. You don’t need to work from scratch. In this article, we will get know-how HTML admin templates used in PHP? Before we start let’s take a brief idea of what is PHP? And why admin template uses?

What is PHP? And why admin template uses?

PHP stands for Hypertext Preprocessor; It is a server-side scripting language. It is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, which earlier stood for Personal Home Pages. Admin template helps you to make development easier because it contains ready-to-use pages and elements (like widgets, charts, and icons) that you can push to prod in almost no time instead of getting bogged down in development from scratch. That’s what templates are. Time-saving at its finest. .With the time saved come reduced costs.

How to Use the HTML Admin template in PHP?

For understanding this concept via example. Let’s take an admin template. I have a gradient able. Step 1: Create an index.php page.

          
Upgrade To Pro

NOTE: Save the file with the .php extension. Step 2: After creating a page add a header section in the page.
In header section contain 2 pages first for the common style sheet and 2nd for the meta tag and external stylesheet. Create a common style sheet via the following code.

Create a meta.php page. It contains the information of Meta title, Meta description; in header section also include the link of the stylesheet. Refer to below code for this:

Step 3: Include loader.php page. It loads the pre-loader normally when you visit a website, your browser starts downloading different parts of the content. To include loader put following code.

Step 4: Create header.php page.
In the header section, there are two different sections: 1)Include siderbar.php. Add following code.

Orders Received

486

Completed Orders351

Total Sales

1641

This Month213

Revenue

$42,562

This Month$5,032

Total Profit

$9,562

This Month$542

Statistics
Customer Feedback
365247

83%

Positive

17%

Negative

    HomeSecurityEntertainmentBig Data
ImageProduct CodeCustomerPurchased OnStatusTransaction ID
prod imgPNG002344John Deo05-01-2017Faild#7234486
prod imgPNG002653Eugine Turner04-01-2017Delivered#7234417
prod imgPNG002156Jacqueline Howell03-01-2017Pending#7234454
ImageProduct CodeCustomerPurchased OnStatusTransaction ID
prod imgPNG002653Eugine Turner04-01-2017Delivered#7234417
prod imgPNG002156Jacqueline Howell03-01-2017Pending#7234454
ImageProduct CodeCustomerPurchased OnStatusTransaction ID
prod imgPNG002413Jane Elliott06-01-2017Shipping#7234421
prod imgPNG002156Jacqueline Howell03-01-2017Pending#7234454
ImageProduct CodeCustomerPurchased OnStatusTransaction ID
prod imgPNG002413Jane Elliott06-01-2017Shipping#7234421
prod imgPNG002344John Deo05-01-2017Faild#7234486

8.62k Subscribers

Your main list is growing

+40 Followers

Your main list is growing

Business Plan

This is your current active plan

Profile
User-Profile-Image
Alessa Robert

Active | Male | Born 23.05.1992


Activity Level: 87%

1256

8562

189

Lorem Ipsum is simply dummy text of the printing and typesetting industry.


Activity Feed
    User-Profile-Image
    File Eddie uploaded new files: 2 hours ago

    hii @everone Lorem Ipsum is simply dummy text of the printing and typesetting industry.

    img
    Old Scooter

    PNG-100KB

    img
    Wall Art

    PNG-150KB

    img
    Microphone

    PNG-150KB

    User-Profile-Image
    TaskSarah marked the Pending Review: Trash Can Icon Design2 hours ago
    User-Profile-Image
    comment abc posted a task: Design a new Homepage6 hours ago

    @everone Lorem Ipsum is simply dummy text of the printing and typesetting industry.

    User-Profile-Image
    TaskSarah marked : do Icon Design10 hours ago

Источник

Оцените статью