we are working on a project and it our prof said we need to use redirect; One for the user and one for the admin, if we type the user it should redirect to user.php and if we login admin it should redirect to admin.php but header location doesnt work. Why?
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbnew";
$iusername = $_POST['username'];
$ipassword = $_POST['password'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Select * FROM acc WHERE username='$iusername' AND password='$ipassword'";
$query = mysqli_query($conn,$sql);
$numrows = mysqli_num_rows($query);
if ($numrows!=0) // if numrows is not 0 it means username and password is correct
{
while($row = mysqli_fetch_assoc()){
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
}
if ($iusername=='admin' && $ipassword=='admin') // if user is found and user is admin then redirect to admin page
{
$_SESSION['Current'] = "admin" ;
echo header('location:admin.php');
}
else //if user is found and it is not admnin this code will run
{
$_SESSION['Current'] = $dbusername ;
echo header('location:user.php');
}
}
mysqli_close($conn);
?>
Do not use echo header('location:admin.php');
instead use:
header('Location: admin.php');
Remember you not echo out / print anything before calling header function()
Please refer to the header function manual for further reading. http://php.net/manual/en/function.header.php
There is no need to use the echo header('location:file.php');
Only
header('location:admin.php');
header('location:user.php');
is fine.