Jilfae Saved Andrews
Jilfae Saved Andrews

Reputation: 11

How do i write restrict access to a page?

I wrote custom login script for users to login to the main control page. I found out that even when users are not login they can still visit the main control of which I want someone to help me to write a restrict access to page().

Please look through my php login script and based on that code help me write the restrict access to the main control page. assume that my main crontrol page is: cecontrolpage.php

I know we use $_SESSION to that but I have little idea of it.

this is my login.php code which is working fine:

<?php
Session_start();
$Email       = $_POST["email"];
$Password    = $_POST["password"];
$cn          = "localhost";
$db_username = "root";
$pas         = "***";
$db_name     = "cemembers";
//Open a connection to a MySQL Server
if ($Email && $Password) {
    $connect = mysqli_connect($cn, $db_username, $pas, $db_name) or die("Could not connect to database");
    //sending MySqli query
    $query   = mysqli_query($connect, "SELECT * FROM users WHERE Email= '$Email'");
    $numrows = mysqli_num_rows($query);
    //After PHP declaration we are going to create index file[form]
    if ($numrows !== 0) {
        while ($row = mysqli_fetch_array($query)) {
            $dbEmail    = $row["Email"];
            $dbPassword = $row["Password"];
        }
        if ($Email == $dbEmail && $Password == $dbPassword) {
            header("location:ce membership birthday system control_pannel.php");
            @$_SESSION("Email") == $Email;
        } else
            header("location:index.php?login_attempt=1");
    } else
        header("location:index.php?login_attempt=2");
} else
    header("Location:index.php?login_attempt=0");
?>

please can someone help me write the php code to restrict access to cecontrol.php ??

Please STEP by STEP with php comments on each part.

Upvotes: 0

Views: 155

Answers (2)

kya
kya

Reputation: 1828

First you need to check if the user is logged in: you do that by checking if the session has been set.

//Check if the user is logged in
    function userlogged_in()
    {
        return(isset($_SESSION['userid']))?true:false;
    }

Then you need to redirect the user to a page that says the access to is not authorised, they need to be logged in to view that page:

You do this by checking if the userlogged_in function returned a true or false

function user_restricted()
{
    if (userlogged_in()=== false)
    {
        header('Location: permission.php ');
        exit();
    }
}

Then you need to call the user_restricted() function on each page, just after starting the session.

Upvotes: 3

Kamran
Kamran

Reputation: 2741

First you have to save user values in session after authentication from database like
$_SESSION['username'] = "name"; $_SESSION['user_id'] = 1;

function check_session() {
                            session_start();
                            if ($_SESSION['user_id']=='')
                            {
                               // redirect to login 
                            }
                        }

On every page that you want to restrict access you can call check_session().

Upvotes: 0

Related Questions