-1

I am recently building a simple php login page with mysql. When I test my site on the localhost it works fine, however when I upload it to the server it cannot redirect to the following page and keep showing the same login page. I noticed that when I enter the login page chrome keeps warning me the site is not secure.

the following code is for redirecting the page after password comparison. Can some one help me with this problem? Thanks!

if (password_verify($_POST['password'], $hash)) {


    $_SESSION['logged_in'] = 1;
    $new = stripslashes($program);
    header("location: $new.php");
}
else {
    $_SESSION['message'] = "Wrong password, try again!";
    header("location: error.php");
    echo "fails";
}
}
chris85
  • 23,846
  • 7
  • 34
  • 51
Leon Meng
  • 1
  • 1
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Qirel Aug 01 '17 at 19:39
  • Could also be a Cookie issue. Check if the server is responding with different cookies every time. – QuickDanger Aug 01 '17 at 19:40
  • Chrome's warning isn't relevant to the question. That's an SSL certificate issue. – JBH Aug 01 '17 at 19:41
  • I'm assuming the extra `}` at the end of your example is a copy-and-paste artifact. Have you looked at the `error_log` file for your site? – JBH Aug 01 '17 at 19:47

2 Answers2

-1

use this function to resolve problem

function redirect($url){
    if (headers_sent()){
        die('<script type="text/javascript">window.location.href=\'' . $url . '\';</script>');
    }else{
        header('Location: ' . $url);
      die();
    }
}
aidinMC
  • 1,415
  • 3
  • 18
  • 35
  • @Qirel the question title is so illusory :| – aidinMC Aug 01 '17 at 19:50
  • Indeed it is! I find the question a bit unclear without any additional information. But I wouldn't ever use a redirect function like this ;-) – Qirel Aug 01 '17 at 19:53
  • @Qirel I would be so happy if you are teach me the better way :) – aidinMC Aug 01 '17 at 20:07
  • Well, a php redirect should only be done before output (I wouldn't rely on javascript at all). That means structuring the code in a way that control structures that checks if a redirect should be made is placed before output ;-) – Qirel Aug 02 '17 at 07:53
-1

I use header("Location: ".$location."/"); regularly and it works fine. I notice that I'm capitalizing Location and you are not. The HTTP protocol is quirky about capitalizations. I might be wrong, but capitalize the "L" and see what happens.

JBH
  • 1,823
  • 1
  • 19
  • 30