0

Done a login form using php mysql. Its working fine in windows plesk. Migrated from plesk windows to cpanel, header location redirect stopped working. However it check login with db

     <?php
        session_start();

                if(isset($_POST['submit'])){

                    $name1=$_POST['username'];  
                    $pass=$_POST['password'];



                    include("connection.php");



                if ($stmt = $conn->prepare("SELECT id,username,password from login WHERE BINARY username = BINARY ? and BINARY password = BINARY ?")) {
$stmt->bind_param("ss", $name1,$pass);
$stmt->execute();
$stmt->bind_result($id,$name1,$pass); 
 if ($stmt->fetch())     
 {
     $_SESSION['login']=1;
     $_SESSION["loginname"] = $name1;
     $_SESSION["idname"] = $id;

     header("Location:dashboard.php"); 

 }


 else
 {
    ?> <p class="text-center" style="margin-top:12px;color:red;"><b>Invalid Username / Password !</b></p>

    <?php
 }

}

                }


            ?>

Help me sort out the issue.

Mohamed
  • 23
  • 1
  • 9

3 Answers3

3

It's due to the spaces at the beginning of the file.

Make sure there is nothing out of <?php and ?> before the redirect is done as the headers should be sent before any content of the page has been sent. If you enable the php warnings there should be a warning about this. Also make sure there is nothing printed in the included connection.php file before the redirect is done.

In your case some spaces have been sent so that the header redirection will not be done.

It could be still done if the web server or the script had an output buffer but since you say it's not working it doesn't seem to be the case.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Will you able to explain me clearly in my scenario where the space have been sent. – Mohamed Jun 07 '20 at 09:39
  • 1
    In the example you have posted in your question there are multiple spaces before the `) in there and also in the included `connection.php` file. You can also [enable the php errors](https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings) to see the warning about this – jeprubio Jun 07 '20 at 09:49
0

Added <?php ob_start() ;?> in top of the page which solved the issue

Also added ob_end_flush(); to clear the buffer.

Mohamed
  • 23
  • 1
  • 9
  • That's one of the output buffers I was talking about. But it's a patch, not a fix. Because you should find first what output had your pages before sending the headers to know the real cause and fix it well. In case you really don't see content outside the php marks keep in mind that it could also be due to [a BOM character](https://stackoverflow.com/questions/2558172/utf-8-bom-signature-in-php-files) in the php files – jeprubio Jun 07 '20 at 13:31
-1

Try this

echo "<script>setTimeout(\"location.href = 'dashboard.php';\",000);</script>";
Ravi
  • 5
  • 1