简体   繁体   中英

After successful login page is not redirecting to new page in php code

I create login system in php.when i fill username and password,session start but page is not redirecting to profile.php which i wants after login and login page is refreshing itself.but when i manually refresh login.php then it redirect to profile.ph. my login code is:

if ($result->num_rows != 1) {
    //echo "Invalid credentials...!";
    $message = "wrong credentials";
    echo "<script type='text/javascript'>alert('$message');</script>";
} else {
    // Authenticated, set session variables
    $user = $result->fetch_array();
    $_SESSION['user_id'] = $user['id'];
    $_SESSION['username'] = $user['username'];


    $message = "You have successfully logged in..";
    echo "<script type='text/javascript'>alert('$message');</script>";

    redirect_to("profile.php?id={$_SESSION['user_id']}");

}

use

redirect("profile.php?id={$_SESSION['user_id']}");

or

http_redirect("profile.php", array("id" => "$_SESSION['user_id']"), true, HTTP_REDIRECT_PERM);

or

header("profile.php?id={$_SESSION['user_id']}");

Some Useful Links

  1. header
  2. http_redirect
  3. How to make a redirect in PHP?

PHP Header Redirects do not work after the output is sent. And you are sending it here

$message = "You have successfully logged in..";
echo "<script type='text/javascript'>alert('$message');</script>";

According to your comment, your function indeed uses a header redirect.

function redirect_to ($url) { header("Location: {$url}"); }

That won't work for reasons explained above.

Use a javascript redirect there.

  echo "<script>location.href='profile.php?id={$_SESSION['user_id']}';</script>";

As mentioned in the comments to your question, the header redirect needs to be the first thing the browser does. So you need remove the echo statement.

if ($result->num_rows != 1) {
    //echo "Invalid credentials...!";
    $message = "wrong credentials";
    echo "<script type='text/javascript'>alert('$message');</script>";
} else {
    // Authenticated, set session variables
    $user = $result->fetch_array();
    $_SESSION['user_id'] = $user['id'];
    $_SESSION['username'] = $user['username'];

    redirect_to("profile.php?id={$_SESSION['user_id']}");
}
<?php
ob_start ();
if (isset ( $_POST ['submit'] )) {

    $email = $_POST ['email'];  
    $password = $_POST ['password'];
    $sql = "SELECT * from users WHERE email='$email' AND password='$password' limit 1";
    $result = mysql_query ( $sql );
    $num = mysql_num_rows ( $result );
    if ($num > 0) {
        while ( $rows = mysql_fetch_array ( $result ) ) {

            $username = $rows ['username'];
            $uid = $rows ['user_id'];
        }
        session_start ();
        $_SESSION ['user_id'] = $user ['id'];
        $_SESSION ['username'] = $user ['username'];
        header ( "location:profile.php?id=" . $_SESSION ['user_id'] );
    } else {
        echo "<p><b>Error:</b> Invalid username/password combination</p>";
    }
}
?>

if you are handling this php code in your login page itself then please give full code with html. Because automatic refreshing may be a problem with html code itself.

Please paste full code, otherwise try replacing

redirect_to("profile.php?id={$_SESSION['user_id']}"); 

with the following line..

header("location:profile.php?id=".$_SESSION['user_id']);

don't forget to add the following line at top of your php code.

ob_start();

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