Admin php admins add

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.

Читайте также:  Java private key to pem

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.

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

Источник

Simple Admin Login PHP Script

This is an Admin Login script made with PHP.
It is useful if you want to implement in your site a simple admin login system, without database.
The users are added manually by the Administrator, in the php code of this script. You can add multiple admin-user accounts with a Rank number to each one, so, then you can display content for loged Admin in your site according to its rank.

• To download the script, click on this link: Download Admin Login Script (4 KB).

Code of the script

=9) < $content ='Content for Admin with Rank 9+'; >else if($_SESSION['adminrank'] >=5) < $content ='Content for Admin with Rank 5+'; >else < $content ='Content for logged Admin with rank lower than 5'; >> else < $content ='Content for no logged Admin.'; >echo $content;

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL

Address: http://CoursesWeb.net/ - Tutorials.

What JavaScript function can be used to call another function multiple times, to a specified time interval?

function someFunction() < alert("CoursesWeb.net"); >setInterval("someFunction()", 2000);

Last accessed pages

  1. Align DIVs on the same line (7414)
  2. Change CSS file with jQuery (5246)
  3. 101 Zen stories (1670)
  4. Shape Tween — Flash Animation (5854)
  5. Get Mime Type of file or string content in PHP (5505)
  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (526)
  2. PHP Unzipper — Extract Zip, Rar Archives (307)
  3. SHA1 Encrypt data in JavaScript (252)
  4. SHA256 Encrypt hash in JavaScript (234)
  5. Read Excel file data in PHP — PhpExcelReader (228)

Источник

How to Add an Admin User in WordPress Using Functions PHP File?

add admin user wordpress functions php

Admin users of WordPress have full control of the website and all the capabilities and can access everything in the admin area. So in case, you want to add an admin user in WordPress with a code, then you have to write the code in the functions.php file.

So, in this article, we will learn how to add an admin user in WordPress using the functions.php file.

You can do this with FTP (FileZilla) OR through cPanel directly.

Why You Want to Add an Admin User in WordPress Using Code (Functions.php file)

There can be many cases like:

When you forget the username/email address and password, and if your site hacked and you are not able to login to the admin dashboard, then you may need to add a new admin user using code in the functions.php file.

You can also use phpMyAdmin to add an admin user to WordPress using SQL query with have database access. But if you may not familiar with the database and afraid to run SQL queries directly in the database.

Then it’s better to use the functions.php file to add an admin user in WordPress. You have to add a simple function that is super easy and once it is done then you have to remove that function from the file.

Let’s jump on the main point of this article.

Add an Admin User in WordPress Using functions.php file (With FTP)

To add an admin user in WordPress, you need an FTP (File Transfer Protocol) client-server on your computer. You can use FileZilla OR Cyberduck.

OK. Let’s connect to the website through FTP credentials. When you are connected and able to see your website files. You will see the screen below.

FTP - Connected to the Website

Then move on to your active theme’s functions.php file by visit the following path.

I’ll suggest you use a child theme to add the custom code. Here are step by step guide on how to create a WordPress child theme

And here you will see the functions.php file. To edit this functions.php file with right-click on it and then click the View/Edit option. It will open the file in the editor and you can edit it now.

FTP - Edit the functions.php file

When the file opened in the editor, you have to add a custom function code at bottom of the file. So add the below code in your theme’s functions.php file and save it and upload it through FTP.

NOTE: Remember to replcae $username, $password, and $email_address variable’s values with your own values.

The above code will execute when finished the loading of your website from the frontend.

We used the init action hook for this. You can read more about init here.

Now visit the WordPress admin login page and sign with the username and password that you have just created. You will see, you can log in to the admin dashboard area now.

OK. Now open your functions.php file again and remove that function from the file.

NOTE: Don’t worry. It will not remove the user from the WordPress that you have just added.

Add an Admin User in WordPress (With cPanel)

For this method, you must have cPanel access to edit the file of your website.

To add an admin user in WordPress using cPanel, follow the below steps:

cPanel - Edit File

Hope you understand this article about how to add an admin user in WordPress using functions php file. If you still have any questions, you can ask me in the comment section, I’ll help you with that.

Источник

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