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

in #utopian-io6 years ago (edited)

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

Sort:  

Thank you for your contribution @avantika.
After reviewing your contribution we suggest the following points below:

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.
  • There is a lot of information on this subject, try to find something more innovative and contribute to the open source community.
  • It is very important that you take a very detailed tutorial where it contains theory and practice so that the reader can understand what he will explain.

Please follow our suggestions in the next tutorial. Thank you.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin!

So far this week you've reviewed 9 contributions. Keep up the good work!

Hi @avantika!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @avantika!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.24
TRX 0.25
JST 0.039
BTC 96247.25
ETH 3349.36
USDT 1.00
SBD 3.28