https://github.com/avantikadev/code
By reading this tutorial you will learn-
These elements will require to learn this tutorial
Choose one of the following options:
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
<?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.
If you want to set only logged admin can visit the page First do all the previous steps.
adminauth.php in your root folder or any folder.<?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.
<?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.
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.
For example here is my table structure.
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.
<?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.
<?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.