Abhishek Spartan Abhishek Spartan - 4 months ago 11
PHP Question

my header is not working in the php

When the submit button is pressed am not directed to the header even if condition with isset() is used.
Note:This is just a test page so i know storing password is not safe in way in which i have done

my index.php

<?php
include 'connect.php';

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

$query = "SELECT id FROM login WHERE username = '$username' AND password='$password'";
$result= mysqli_query($connection, $query);
$count = mysqli_num_rows($result);

if($count==1){
header("location:welcome.php");
}
else{
echo "invalid credentials";
}
}
?>
<html>
<head></head>
<body>
<form method="post" action="">
<input type="text" name="username" id="username" placeholder = "Username"/>
<input type="password" name="password" id="password" placeholder = "**********"/>
<input type="submit" value="submit" />
</form>
</body>

</html>


connect.php

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'users');
$connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);


welcome.php

<?php

echo"Hello world";

?>


i havent used any sessions ignoring the fact is there anything i should change?

Answer

The server's REQUEST_METHOD is a string, you are comparing it to the _POST array.

Replace this

if($_SERVER["REQUEST_METHOD"]==$_POST){

with

if($_SERVER["REQUEST_METHOD"] == "POST"){

to properly check the server's request method. Then, the rest of your code should run.

Comments