Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
成功登录后,页面不会重定向到php代码中的新页面_Php - Fatal编程技术网

成功登录后,页面不会重定向到php代码中的新页面

成功登录后,页面不会重定向到php代码中的新页面,php,Php,我用php创建登录系统。当我填写用户名和密码时,会话开始,但页面并没有重定向到profile.php,这是我登录后想要的,登录页面正在刷新自己。但当我手动刷新login.php时,它会重定向到profile.ph。 我的登录码是: if ($result->num_rows != 1) { //echo "Invalid credentials...!"; $message = "wrong credentials"; echo "<script type='

我用php创建登录系统。当我填写用户名和密码时,会话开始,但页面并没有重定向到profile.php,这是我登录后想要的,登录页面正在刷新自己。但当我手动刷新login.php时,它会重定向到profile.ph。 我的登录码是:

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

}
使用

一些有用的链接


请粘贴完整代码,否则请尝试替换

redirect_to("profile.php?id={$_SESSION['user_id']}"); 
用下面这句话

header("location:profile.php?id=".$_SESSION['user_id']);
不要忘记在php代码顶部添加以下行

ob_start();
如果您在登录页面中处理此php代码,请提供完整的html代码。因为自动刷新可能是html代码本身的问题。

发送输出后,PHP标头重定向不起作用。你把它送到这里来了

$message = "You have successfully logged in..";
echo "<script type='text/javascript'>alert('$message');</script>";
由于上述原因,这是行不通的

在那里使用javascript重定向

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

正如您的问题的评论中所提到的,头重定向需要是浏览器做的第一件事。因此,您需要删除echo语句

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

您需要显示重定向到函数的代码。您需要使用header函数重定向以获取更多信息,请查看此处函数重定向到$url{headerLocation:{$url};}在重定向到之前,您有一个回音。在将任何内容输出到浏览器之前,必须调用标头。你需要去掉那条线。请看,OP的函数redirect_to使用headerLocation:。。。;根据OP的评论->谢谢您的建议,但没有一个选项与我一起工作。重定向后登录页面不会刷新。请检查if$message,然后重定向页面在任何条件为真后如何在php中刷新页面?是否要刷新页面或重定向页面?问题不在于重定向到,它使用头文件查看OP的注释,但在重定向之前查看回显。您可能需要向OP解释为什么建议ob_也开始。hello every body echo location.href='profile.php?id={$\u会话['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>";
    }
}
?>
$message = "You have successfully logged in..";
echo "<script type='text/javascript'>alert('$message');</script>";
function redirect_to ($url) { header("Location: {$url}"); }
  echo "<script>location.href='profile.php?id={$_SESSION['user_id']}';</script>";
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']}");
}