I'm currently trying to learn PHP, which is difficult. I'm trying to make my own login system (CMS), however I'm encountering some problems during this process. I have been able to create a login script which is working for now, it's not a safe script I know, but it's just for me to learn the process.
I have added in phpmyadmin a new row which indicates what role the user have (1 or 2), 1 is user and 2 is admin. However I have no idea where to start. I assume it should be something like: if user == however I don't know how to start. Could someone help me a little bit?
I want to create something like the:
Normal user: - Home - Invoices - Contact
Admin: - Home - Invoices - admin - Contact
My script so far:
<?php
session_start();
// Verbinding maken met database
require("db.php");
// Functions na klikken 'inloggen'
if (isset($_POST["login"])){
$gebruikersnaam = mysqli_real_escape_string($con, $_POST['gebruikersnaam']);
$wachtwoord = mysqli_real_escape_string($con, $_POST['wachtwoord']);
$selecteergebruiker = "SELECT * FROM gebruikers WHERE username ='$gebruikersnaam' AND password = '$wachtwoord'";
$run_user = mysqli_query($con, $selecteergebruiker);
$check_user = mysqli_num_rows($run_user);
if($check_user>0){
header('location: home.php');
} else {
echo "Onjuist!";
}
}
?>
Here is some hint you can start with....
if($check_user>0){
while($row=mysqli_fetch_array($selecteergebruiker))
{
session_start();
if($row['role']==1)//user is admin
{
$_SESSION['role']='Admin';
}
else
{
$_SESSION['role']='User';
}
}
header('location: home.php');
} else {
echo "Onjuist!";
}
Now in other page you can use $_SESSION['role']; thats it..
Add a column to the sql table called role Set admin role to admin and other users touser than check what's the user role with an if statement
if ( $user['role'] == admin ) {
Echo "Hello admin";
} else {
Echo "Hi user....";
}