Php will not direct to another page .when i use header("Locaion:)"


#1
<!DOCTYPE html>
<html></html>
<body>
<?php
$con=mysqli_connect('localhost','id169218_venki','**********','id169218_mydb');
session_start();
if(isset($_POST['loginbtn'])){
$email=$_POST['email'];
$password=$_POST['pwd'];
$result=mysqli_query($con,'select * from myguests where email="'.$email.'" and password="'.$password.'"');
if(mysqli_num_rows($result)==1)
{
	$_SESSION['email']= $email;
	header("Location:welcome.php");
}
else
	echo"INVALID ACCOUNT";

}

?>

</body>
</html>





AND THIS IS MY welcome.php


<html>
<body>
<?php
function error_found(){
  header("Location: login.php");
}
set_error_handler('error_found');
?>
<?php     
    session_start();     
    echo"welcome  " .$_SESSION['email'];
?>
<br><a href="logout.php">logout</a>
</body>
</html>

#2

Hi @venki14101996!

Try this one instead:

header("Location: /welcome.php", true, 301);


#3

sry this is not working


#4

should i use any other supporting code or header files to use this command


#5

Are you sure it doesnt echo INVALID?


#6

The bug:

Correct
else {
echo"INVALID ACCOUNT";

}


#8

To display PHP errors please go to 000webhost cPanel > Settings > General > Show errors > On> Off > On


#9

yes i done
.i on that


#10

If header isn’t working it is because the php code is under the <!DOCTYPE html>. To resolve this, but the php code on the top of <!DOCTYPE html>

Example :

<?php // the code is on the start of the file
$con=mysqli_connect('localhost','id169218_venki','**********','id169218_mydb');
session_start();
if(isset($_POST['loginbtn'])){
$email=$_POST['email'];
$password=$_POST['pwd'];
$result=mysqli_query($con,'select * from myguests where email="'.$email.'" and password="'.$password.'"'); 
// mysqli is a bit outdated to be honest. consider using PDO
if(mysqli_num_rows($result)==1)
{
	$_SESSION['email']= $email;
	header("Location:welcome.php");
}
else
	die("INVALID ACCOUNT"); // Use the command die for dyin... uhh i mean display something and interrupt the code.
}

?>
<!DOCTYPE html>
<html>
<body>


</body>
</html>

Here you go :smiley: !


#11

I done that but it is not working

<?php     
    session_start();
    $_SESSION['name']='Guru';     
    echo $_SESSION['name'];
    header("Location:logout.php");
?>

this page contain php element only but this will not redirect me to the logout page


#12

Can you add a space between Location: and logout.php


#14

Whoops, forgot a semicolon in die([...] !