0

Basically I try to make this code to navigate in a different php page.. Thi is verify_user.php

$con = mysqli_connect("localhost", "root", "****", "myDB");
if (!$con){
    die("Connection to database failed: " . mysqli_connect_error());
}

$uname=$_POST['u_name'];
$pass=$_POST['pass'];

$qry = mysqli_query($con, "SELECT * FROM login WHERE user='$uname'");

if(!$qry){
    die("Query Failed: ". mysql_error());
} else {
    $row = mysqli_fetch_array($qry);

        if($_POST['u_name'] == $row["user"] && $_POST['pass'] == $row["password"]) {
            if ($_POST['u_name'] = "admin") {
                session_start();
                $_SESSION['name'] = $_POST['u_name'];
                header("Location:admin_panel.php");
            } else {    
                session_start();
                $_SESSION['name']=$_POST['u_name'];
                header("Location:main.php");
            }       
        } else {
            header("Location:main.php?id=Worng ID / Password!");
        }
    }
    ?>

From this code as we can see, if the user is admin, it should go to admin_panel.php. And if the user is not admin, its should go to main.php. For further explanation; thi is my admin_panel.php

<?php
session_start();
if(isset($_SESSION['name'])){
   if(!$_SESSION['name']=='admin'){
?>



<!-- HTMML CODE -->



<?php
   }
   else
      header("Location:index.php?id=Only for admin.");
}
else
{
header("Location:index.php?id=Only for admin.");
}
?>

But its not working...

Alireza
  • 5,444
  • 9
  • 38
  • 50
Alieym
  • 13
  • 1
  • 1
  • 6

1 Answers1

0

Change

if ($_POST['u_name'] = "admin") {

To

if ($_POST['u_name'] == "admin") {

Change

 if(!$_SESSION['name']=='admin'){

To

 if($_SESSION['name']=='admin'){

In fact you could change

<?php
session_start();
if(isset($_SESSION['name'])){
   if(!$_SESSION['name']=='admin'){
?>



<!-- HTMML CODE -->



<?php
   }
   else
      header("Location:index.php?id=Only for admin.");
}
else
{
header("Location:index.php?id=Only for admin.");
}
?>

To

<?php
session_start();
if(isset($_SESSION['name']) && $_SESSION['name'] == 'admin'){
?>



<!-- HTMML CODE -->



<?php
}
else
{
header("Location:index.php?id=Only for admin.");
}
?>

Also, this really isn't great

header("Location:index.php?id=Only for admin.");

You shouldn't have spaces in an url

And BTW this code would be vulnerable to SQL injections.

Tom Tom
  • 3,680
  • 5
  • 35
  • 40