0

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']}");

}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

5 Answers5

0

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?
Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • it looks like the OP's function `redirect_to()` uses `header("Location:...");` per the OP's comment -> http://stackoverflow.com/questions/32344612/after-successful-login-page-is-not-redirecting-to-new-page-in-php-code#comment52563313_32344612 – Sean Sep 02 '15 at 04:47
  • thanks for suggestions but no one option is working with me.login page is not refreshing after redirection . – Pravin Deshmukh Sep 02 '15 at 04:47
  • check with if `$message` and then redirect page – Abdulla Nilam Sep 02 '15 at 04:49
0

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>";
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

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']}");
}
AJefferiss
  • 1,628
  • 1
  • 13
  • 17
-1
<?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.

phpnerd
  • 850
  • 1
  • 10
  • 25
-2

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();
phpnerd
  • 850
  • 1
  • 10
  • 25
  • query($sql); if ($result->num_rows != 1) { echo "

    Error: Invalid username/password combination

    "; } else { // Authenticated, set session variables $user = $result->fetch_array(); $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; header("location:profile.php?id=".$_SESSION['user_id']); } } ?>
    – Pravin Deshmukh Sep 02 '15 at 04:57
  • how to refresh page in php after any condition is true? – Pravin Deshmukh Sep 02 '15 at 05:36
  • Do you want to refresh the page or redirect the page? – phpnerd Sep 02 '15 at 06:48
  • The problem isn't with the `redirect_to`, which uses `header` see the OP's comment, but the `echo` before the redirect. You might want to explain to the OP why you're suggesting `ob_start` too. – AJefferiss Sep 02 '15 at 08:57