User And Admin Login System In PHP MySQL Step By Step | PHP Tutorial For Beginners (2023)

User And Admin Login System In PHP MySQL Step By Step | PHP Tutorial For Beginners (2023)

Web Tech Knowledge

3 года назад

213,516 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Web Tech Knowledge
Web Tech Knowledge - 26.05.2021 20:29

The source Code :

login.php

<?php

$host="localhost";
$user="root";
$password="";
$db="user";

session_start();


$data=mysqli_connect($host,$user,$password,$db);

if($data===false)
{
die("connection error");
}


if($_SERVER["REQUEST_METHOD"]=="POST")
{
$username=$_POST["username"];
$password=$_POST["password"];


$sql="select * from login where username='".$username."' AND password='".$password."' ";

$result=mysqli_query($data,$sql);

$row=mysqli_fetch_array($result);

if($row["usertype"]=="user")
{

$_SESSION["username"]=$username;

header("location:userhome.php");
}

elseif($row["usertype"]=="admin")
{

$_SESSION["username"]=$username;

header("location:adminhome.php");
}

else
{
echo "username or password incorrect";
}

}




?>









<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<center>

<h1>Login Form</h1>
<br><br><br><br>
<div style="background-color: grey; width: 500px;">
<br><br>


<form action="#" method="POST">

<div>
<label>username</label>
<input type="text" name="username" required>
</div>
<br><br>

<div>
<label>password</label>
<input type="password" name="password" required>
</div>
<br><br>

<div>

<input type="submit" value="Login">
</div>


</form>


<br><br>
</div>
</center>

</body>
</html>









adminhome.php



<?php
session_start();


if(!isset($_SESSION["username"]))
{
header("location:login.php");
}

?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<h1>THIS IS ADMIN HOME PAGE</h1><?php echo $_SESSION["username"] ?>

<a href="logout.php">Logout</a>
</body>
</html>




userhome.php


<?php
session_start();


if(!isset($_SESSION["username"]))
{
header("location:login.php");
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<h1>THIS IS USER HOME PAGE</h1><?php echo $_SESSION["username"] ?>

<a href="logout.php">Logout</a>

</body>
</html>





logout.php




<?php
session_start();
session_destroy();
header("location:login.php");

?>

Ответить
Ken Aquino
Ken Aquino - 02.10.2023 09:48

Great Tutorial! Thank you!

Ответить
murshi shihad
murshi shihad - 12.08.2023 06:09

How i correct 'Trying to access array offset on value of type null in ...'??????pls give me solution......

Ответить
Dinesh Devarapalli
Dinesh Devarapalli - 02.08.2023 09:57

Thank you so much bro❤

Ответить
Luqman Qader
Luqman Qader - 23.07.2023 23:23

Many Thanks

Ответить
manikanta j
manikanta j - 21.07.2023 15:20

waste of time the code is running in your system but not working others system

Ответить
O.k.s...
O.k.s... - 14.07.2023 18:02

Why i got this error ??
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\SIP23PROJECT\User.php on line 25

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\SIP23PROJECT\User.php on line 30
username or password incorrect

Ответить
killan sinyangwe
killan sinyangwe - 13.07.2023 16:55

Please kindly help on how i can login to admin password from a database

Ответить
Ada Nnenna
Ada Nnenna - 11.07.2023 04:01

Thank you for this but please, when I login in, I don't see the username displayed
And when I add the Access denied code for non logged in users, even a registered account is redirected to the login page

Ответить
Linda Keshie
Linda Keshie - 19.06.2023 16:47

Can I have this same one done in notepad

Ответить
MrTonyTv
MrTonyTv - 13.05.2023 15:28

men it was good tutorial but if i login as a user and i type the admin homepae url i can go in

Ответить
Saloni.
Saloni. - 05.05.2023 23:17

spent the entire day looking through countless videos to create a login system to no avail. So glad that I found your video! I was able finish the whole thing from scratch within 30 mins. :)

Ответить
Franz McLay
Franz McLay - 03.04.2023 05:11

elseif($row["usertype"]=="admin")
{

$_SESSION["username"]=$username;

header("location:adminhome.php");
}


PLEASE HELP ME, IM ERROR with that line
it gives this warning "Warning: Trying to access array offset on value of type null"

Ответить
Zoldyck
Zoldyck - 02.04.2023 09:44

can we use the same php code and relate it with android studio app please help

Ответить
Zoldyck
Zoldyck - 02.04.2023 09:29

is it the same for android studio app?

Ответить
jogani darshil
jogani darshil - 11.03.2023 23:54

this short video tutorial was really helpful. Thank you.

Ответить
Athirah __
Athirah __ - 22.01.2023 21:25

Can u tell me how can i change the name of the user_type bcs i have done it but the error keeps coming

Ответить
Nicholas Tolores
Nicholas Tolores - 18.01.2023 13:31

Can I ask what tools or software I need for programming

Ответить
ерой
ерой - 16.01.2023 08:20

oke

Ответить
AHMAD BIN ABAS KPM-Guru
AHMAD BIN ABAS KPM-Guru - 31.12.2022 04:24

SIR SHARE CODING

Ответить
manju thenga
manju thenga - 30.12.2022 13:47

hai, any one can be login as user or admin

Ответить
baadshah cults💥🤙
baadshah cults💥🤙 - 09.12.2022 18:26

Thank you sooo much🙏🙏

Ответить
Harvey
Harvey - 20.11.2022 17:04

Since you are only checking to see if the username session variable is set, once you login as a regular user, you can then type the URL for the admin home page and gain access.

Ответить
Kenil Gsja
Kenil Gsja - 31.10.2022 21:27

sir everything is easy to understand i like this video it is in full detail

Ответить
KENYAN MEMES
KENYAN MEMES - 30.10.2022 06:35

Hello, how do I get my xammp port to work, it says port 3306 has been occupied

Ответить
Christine Marie Manus
Christine Marie Manus - 18.10.2022 15:48

Thank you for sharing your knowledge.

Ответить
Jacky Joshua lim
Jacky Joshua lim - 14.10.2022 13:44

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\CAPSTONE\loginpractice\login.php on line 31

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\CAPSTONE\loginpractice\login.php on line 39

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\CAPSTONE\loginpractice\login.php on line 47


can someone help me with this error asap

Ответить
TEAM NORTE TH
TEAM NORTE TH - 12.10.2022 08:40

Can i request sir ...can you please make an municipal library system sir thank you and godbless

Ответить
TEAM NORTE TH
TEAM NORTE TH - 12.10.2022 08:39

Thank you very much my friend its work

Ответить
mostafa abd el rhman
mostafa abd el rhman - 08.09.2022 14:15

hello how i can change user to be an admin from backend ?? i know i must make a register page and all data go to database can i know how i make some user be admin not from change his statues from phpmyadmin

Ответить
Nalaka Sajee
Nalaka Sajee - 06.09.2022 12:31

Patta machan

Ответить
jobbi
jobbi - 28.08.2022 03:54

I login into the user account, but if I click on the back, the page goes to admin page . any one have solution?


<?php

include 'config.php';

session_start();

if(isset($_SESSION['username'])){
header("Location: 1index.php");
}

if(isset($_POST['submit'])) {
$email = $_POST['email'];
$password = md5($_POST['password']);

$sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);

if($row["usertype"]=="user")
{
$_SESSION['username'] = $row['username'];
header("location:index.php");
}
elseif($row["usertype"]=="admin")
{
$_SESSION['username'] = $row['username'];
header("Location: 1index.php");
}

} else {
echo "<script>alert('Woops! Email or Password is Wrong.')</script>";
}
}

?>

Ответить
Royce Hortaleza
Royce Hortaleza - 14.08.2022 12:16

Thank you!

Ответить
Meeta Manhar
Meeta Manhar - 04.08.2022 11:12

thank your sir this video very helpful 🙏🙏

Ответить
Pazzo_ Fredus
Pazzo_ Fredus - 12.07.2022 15:04

pretty sure after login in as user you can, type in the adminhome.php and have access to that, because theirs no role check so maybe add this:

adminhome.php

<?php
session_start();

if(!($_SESSION["username"] == admin))
{
header("location:login.php");
}
?>

The same for userhome.php.

Ответить
Said Gheblawi
Said Gheblawi - 04.07.2022 21:51

thank you so much saved my life, liked!

Ответить
JP De Aquino
JP De Aquino - 24.05.2022 17:24

how to fix?
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\project\login.php on line 31

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\project\login.php on line 39

Ответить
GAMING CM
GAMING CM - 16.05.2022 23:45

goood

Ответить
Aniket singh
Aniket singh - 07.05.2022 16:45

Thanks * infinity

Ответить
Mihir Rana
Mihir Rana - 14.04.2022 23:02

bhai helpfullll

Ответить
Just Being
Just Being - 11.04.2022 10:44

If its not too much trouble, please do an online crime reporting system, where the police is the admin

Ответить
Just Being
Just Being - 11.04.2022 10:40

You have taught me alot than my teachers😍😍 thank you so much

Ответить
Rahul Kumar
Rahul Kumar - 06.04.2022 07:46

If we use html , javascript and css for front end then will it work ???

Ответить
Rodrigo Linox
Rodrigo Linox - 05.04.2022 20:51

Incrível!!! Professor parabéns!

Ответить
CABUSOR, ANGE
CABUSOR, ANGE - 04.04.2022 11:19

can you teach us how to make attendance app for student plsss🙏🙏

Ответить
Hathanehru Khandriv
Hathanehru Khandriv - 01.04.2022 20:43

add hw to select data from database

Ответить
Bishaw S
Bishaw S - 16.03.2022 22:56

this is really helpful!

Ответить
Priya Rani4418
Priya Rani4418 - 07.03.2022 19:18

An error in this...... That was... Trying to access array offset on value of type null... Plz give solution

Ответить
MeCyber
MeCyber - 01.03.2022 15:47

I got this warning "Warning: Trying to access array offset on value of type null" with admin account only, what happening? and what should i do?

thank you

Ответить