Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to write PHP code to edit my pages so the link in the home page that says Not available unless you are Admin,

I need to write PHP code to edit my pages so the link in the home page that says "Not available unless you are Admin", can only be accessed by admin. The admin credentials are username ("admin") and password ("dcsadmin01"). Also please note that all credentials are saved in a text file (users.txt). Please have a look on the pages below:

login.php

// session start

session_start();

if(isset($_POST['login'])) // it checks whether the user clicked login button or not

{

$user = $_POST['username'];

$pass = $_POST['password'];

if(isset($_POST["username"]) && isset($_POST["password"])){

$file = fopen('users.txt', 'r');

$good=false;

while(!feof($file)){

$line = fgets($file);

$array = explode(";",$line);

if(trim($array[0]) == $_POST['username'] && trim($array[1]) == $_POST['password']){

$good=true;

break;

}

} if($good){

$_SESSION['username'] = $user;

echo "";

}else{

echo "Invalid UserName or Password";

}

fclose($file);

}

}

?>

Login Page

Login Page
Username
Password
If you want to go back to index page click here

home.php

// session starts with the help of this function

session_start();

$userid='';

//check session is set or not

if(isset($_SESSION["username"])){

//get the username and store in variable

$userid=$_SESSION["username"];

}

else{

header("Location:login.php");

}

?>

Home Page

Welcome:

Logout

DTresults,

P1results, PfPresults Not available unless you are Admin

admin.php

session_start();

$userid='';

//check session is set or not

if(isset($_SESSION["username"])){

//get the username and store in variable

$userid=$_SESSION["username"];

}

else{

header("Location:login.php");

}

// if submit form we get data from $_POST method

if(isset($_POST["username"]) && isset($_POST["password"]))

{

// check if user file exist.

$file=fopen("users.txt","r");

$finduser = false;

while(!feof($file))

{

//get the data in line

$line = fgets($file);

// split username and password and store in array which is save in users.txt file

$array = explode(";",$line);

if(trim($array[0]) == $_POST['username'])

{

$finduser=true;

break;

}

}

fclose($file);

// register user or pop up message

if( $finduser )

{

echo $_POST["username"];

echo ' existed! ';

include 'admin.php';

}

else

{

// save data in text file

$file = fopen("users.txt", "a");

fputs($file,$_POST["username"].";".$_POST["password"]." ");

fclose($file);

// print register username

echo $_POST["username"];

echo " registered successfully!";

}

}

?>

Admin

Register new user
Username
Password

If you want to go to home page click here Or if you want to log out click logout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

Identify and describe basic workplace competencies

Answered: 1 week ago