4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

[Tutorial] Admin Access,Group role Function With Php and MySQL

Repository

https://github.com/avantikadev/code

images (8).jpeg

What Will You Learn?

By reading this tutorial you will learn-

  • You will learn How to make a page which can only access by admin
  • You will learn How to create a page which can only access via a specific user
  • You will learn How to create group system
  • You will learn How to create page accessibility for a specific group

Requirements

These elements will require to learn this tutorial

  • Basic knowledge on php
  • Basic knowledge on MySQL
  • A php based website

Difficulty

Choose one of the following options:

  • Basic

Tutorial Contents

Hi I am @avantika. This is my first web tutorial contribution on utopian-io. I am a web developer. Today I will tech you some php function. So lets start.

Every php site has database connection file like, function.php, connect.php or other. This file has MySQL database connection system. It is nessesary for our tutorial. If your file has some problem download my function.php file from my github profile. You can find it here


Tutorial-1 : User access system
  • At first login to your website root folder.
  • Copy the file name of database connection system.
  • Write the bellow code into the head tag of your page. or into the header section or header file.
<?php
require_once('../function.php');   
connectdb();
session_start();

/* Edit "function.php" to your database connection file */


if (!is_user()) {
	redirect('index.php');  
}
	/*change the "index.php" to any link where you want to redirect him if he is not user */
?>

This is the function for showing a page to all registered and logged user. You should change the "function.php" and "index.php" link.

Tutorial -2: Only admin access mode.

If you want to set only logged admin can visit the page First do all the previous steps.

  • Then create a file named adminauth.php in your root folder or any folder.
  • write the bellow code in the file and save it.
<?php
require_once('../function.php');   
connectdb();
session_start();

/* Edit "function.php" to your database connection file */

 $user = $_SESSION['username'];

 if($user!="admin"){
	redirect('signout.php');
 }
 /* change the line -3  "admin" to your admin username. 
 Edit "signout.php" to your signout link */
 
? >

You should change the admin username to your admin username or the user whom you want to make admin.

You can add multiple admin with comma. like "admin","controller","moderator","mod1"; etc.

  • Now go to the page where you want to set only admin can access the page.
  • Write down the code into the head section of the page
<?php 
include ('adminauth.php'); 
?>

Now visit the page without login with admin username.
You can not access the page!
Login with admin username and visit the page.
Wow you can access the page.

Tutorial -3 : Only access by specific group

Do you want to create an admin/moderator/other groups. and set a page accessibility for only one or more groups. You can try this tutorial.

  • Login to your MySQL databse. open the table where user details are hosted.

For example here is my table structure.

IMG_20181014_172534_624.JPG

You can see that there is many columns. Like; id,username,password,phone,sid etc.

Your user information table also has this kind of column.

  • Now create a new column with column name "role". Set it to varchar(255) and default value to "user"

  • Now you can see that every users role is "user".

  • Now change the role name to "admin" or other group name.

MySQL work finish.

  • Now we should go to the root folder and create a folder name "group" and inside the folder create file with named "(your group/role name).php" like. admin.php /mod.php / other name.php
  • Edit all files with bellow code.
<?php
require_once('../function.php');   
connectdb();
session_start();
/* change the "function.php to your database connection page */

$group = mysql_fetch_array(mysql_query("SELECT username FROM users WHERE role='admin'"));

/* change "admin" to role name */

<?php
 $user = $_SESSION['username'];

 if($user!="".$group.""){
    redirect('signout.php');
 }
?>

now save the file.

  • Go to the page where you want to set only specific group can access this page.
  • write down this code into the head section.
<?php include('group/rolename.php'); ///change the role name to your page name where you write the previous code #
?>

save the file.

Now see the magic only specific groups user can access the page.

If you have any problem. Comment here. Or if you need the user login/registration system. Comment here. I will post the next tutorial.

Proof of Work Done

https://github.com/avantikadev/code

23 upvotes $5.87

Replies (4)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: