0

I am making my own login system. What I am doing:

  1. index.php: i have a form where i have fields to login
  2. 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

  • if you are using `include` replace with `include_once` for your first Problem and also use `isset` for checking form post – helpdoc Dec 01 '17 at 08:21
  • Welcome to `StackOverflow`, please provide a [MCVE](https://stackoverflow.com/help/mcve). – Hille Dec 01 '17 at 08:22
  • @Hille i added some code to clear up the question – user9032716 Dec 01 '17 at 08:26
  • @SNTiwari i don't know what you are talking about, i am a novice programmer – user9032716 Dec 01 '17 at 08:27
  • The solution is simple: Put all your login PHP code **at the top of the file before any HTML output.** Redirect there if necessary. Don't start processing the login which may potentially end in a redirect **in the middle of your HTML.** It's too late there. – deceze Dec 01 '17 at 09:05

0 Answers0