Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
probleme with header("Location ")

the redirect not allways work with me , in my situation I didn't found solution so I put a redirect using javascript code, can someone tell me what's wrong with my code and why the echo don't work also , I want echo message to the user when password or login is not correct, thanks

function getlogin($conn) {
    if (isset($_POST['loginsubmit'])) {
        $uid = mysqli_real_escape_string($conn, $_POST['uid']);
        $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

        $sql = "SELECT * from test where uid='$uid'";
        $result = $conn->query($sql);
        $row = $result->fetch_assoc();
        $hash_pwd = $row['pwd'];
        $hash = password_verify($pwd, $hash_pwd);

        if($hash == 0) {

            header("Location : test.php?error=empty");
        }



          else {

        $stmt = $conn->prepare("select * FROM test WHERE uid=? and pwd =?");
        $stmt->bind_param("ss", $username, $password); 

        $username = $uid;
        $password = $hash_pwd; 
        $stmt->execute();

        $result = $stmt->get_result();
        $rowNum = $result->num_rows;


        if ($rowNum > 0) {
            if ($row = $result->fetch_assoc()) {
                 $_SESSION['id']  = $row['id'];
                 echo "<script> window.location.replace('test.php') 
   </script>" ;
                 exit();
          }
              }else {
                  header("Location : test.php");
                  echo " echo don't work "; 
                 exit();


              }
         }
    }
    }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

<script type="text/javascript">window.location.href = 'link here';</script>

Don't recommend the use of the header () function after the start of the page. Use this code in Javascript that works well and does not generate annoying errors for you.

over 4 years ago · Santiago Trujillo Report

0

In majority of cases, the reason why header redirect doesn't work is that the page printed something before the header was called. It can be as simple as a single empty line, or the html tags that start a page. You should check that your sql isn't returning an error, because if there is an error message before the header, it is going to cause a warning and block a redirect. In some systems, it is set to tolerate the warning and redirect anyway, so it could be that it works locally despite of the error but when you put it in a more strict environment, it causes an error. You should temporarily comment out the redirect and see what is on the page when it stops instead of redirecting. If there is an error message, it's easy to find where the problem is. If there is an empty line, it might be harder to hunt down. Also I'm not sure if it's okay to put a space before the : after header. I've never done that. Also does your test table have both id and uid that are two separate things? Where uid is the user id and id is the session id? Because if you don't, that could be a typo.

Also, you can't put an echo after a redirect because you get redirected to another page before the echo is ever executed. So you should redirect to a page that says your login is incorrect instead of putting the echo in your function. If you don't want the page to always print that, add something like ?error=1 in the redirect, then have the page only print the message if error is set and is 1.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!