简体   繁体   中英

My header() call is not redirecting to new page

I am having problems getting my page to redirect using the header method. I think I know what the problem is. But I am very new to the PHP scene so I don't know what else to try. From what I have read the header() method will not fire if a page has already sent data prior to the header. I feel this could be part of my problem because I have several echo statements before my header function. Could it be something else? How do I get around this?

An echo statement proves that the correct username and password are being located. I put a test echo statement where the header is, and it works just fine. So, I know the database is finding the correct information. The page refreshes when the info is inserted. Thanks for looking.

<div id= "sidebar" >
    <div id="login-box">    
        <form name="client-login" action="<?php $_SERVER['PHP_SELF']; ?>"  method="post">
            <label id="user-lbl">Username:</label><br /><input type="text"
                name="username" id="username" /><br /> <label id="pass-lbl">Password:</label><br /><input
                type="password" name="password" id="password" /><br />     <label>&nbsp;</label><input
                type="submit" value="Login" class="submit" id="login" />
        </form>     
<?php 
$username = $_POST['username'];
$password = $_POST['password'];

if((!isset($username)) || (!isset($password))){

    echo "<p>Username is $username. Password is $password.</p>";

     }else{

    $mysql = mysql_connect("myhost", "username", "password");
    if(!$mysql){
    echo "<p>Cannot connect to database!</p>";}
    $selected = mysql_select_db("mydb", $mysql);
    if(!$selected){
    echo "<p>Cannot find database!</p>";}

    $query = sprintf("SELECT count(*) FROM users WHERE username = '%s' AND password = '%s'", mysql_real_escape_string($username),mysql_real_escape_string($password));

    $result = mysql_query($query, $mysql );
    if(!$result){
    echo "<p>Cannot run query.</p>";}

    $row  = mysql_fetch_row($result);
    $count = $row[0];
    if ($count > 0){
    header("Location: account.php"); die();} else {echo "<p>Invalid username or password!</p>";}
}  
?>

    </div><!-- #login-box -->

If you call header() , then it should be called BEFORE any other function that prints an output to the HTML stream, OR before any HTML statements in that file. You cannot send even a newline character before the header() call.

Here in your case, first you are sending the <div id= "sidebar" > and other 7 lines to the client, and then only you are calling the header() function. That's why the error happened to you.

Reason:

The header() call is meant to deliver the HTTP header information to the client browser. The header information should be the first information to be sent to the client. If you are not calling the header() in your PHP script, the PHP will automatically send a header information just before sending the very first line of your PHP file.

Once a header is sent, you cannot send another header in the same PHP file. So, if you want to send a header exclusively, send it before any other body content starts.

Yes, that's your problem.

The best solution for this is to have the code execute before you output anything to the browser. Barring that, adding ob_start(); to the top of your script will buffer your output, allowing a header(); call to work even after you've printed/echoed text.

By using php code:

header('page url');

Using header may make problems while uploading to server.

By using javascript:

window.location='page url';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM