I am making my own login system. What I am doing:
- index.php: i have a form where i have fields to login
- register.php: input details of new user and store it in data base
Now my problem is when i have to log my user in through index.php
- i can set the form action to the same file but if the login credentials match in the database, and i use header to redirect my user ot login page but i get the following error Warning: Cannot modify header information - headers already sent by (output started at And i guess that's because there are other things on my page other than the php code itself
- the other way round i can go make another page and i can set my action to that page where i again check if the credentials are wrong and if they are (now here's the problem) i need to print an error message but the thing i get if i echo out my error message, what i get is ONLY the error message, i want the error message to be printed below the form in index.php
How can i achieve this?
Here is the php script i am writing after the form in index.php
<?php
require 'dbconfig/config.php';
if(isset($_POST['sbmt-btn']))
{
//echo "<script type='text/javascript'>alert('clicked');</script>";
$email=$_POST['email'];
$pass=$_POST['password'];
$query="select * from user WHERE username='$email' AND password='$pass'";
$query_run=mysqli_query($con,$query);
if(mysqli_num_rows($query_run)>0)
{
$_SESSION['email']=$email;
header('location:loggedIn.php');
}
else
{
echo "<div class='alert alert-danger'><strong>Error!</strong> Incorrect username or password </div>";
}
}
?>
where i get the error mentioned in point number 1 and if i put this code in a different file and put the action to that file and if the login details are wrong, i neeed to go back to index.php to display an error message
WORKAROUND i made an extra session variable if the login credentials were wrong in file where i shifted the above mentioned code and set it to wrong and i am checking in index.php if it is set and its value is wrong then i am displaying the error message but that too is not working