如何在php和JavaScript中重定向phpWindow.location.href或Window.open()方法

How redirect php Window.location.href or Window.open () methods in php and JavaScript

我按照教程学习更多关于 php 的信息,在它的源代码中有一些东西当时似乎可以工作,但现在不再工作了。这是代码,请让我知道我应该在代码中更改什么以使登录过程正常工作(目前在输入有效用户名并通过并单击登录后它冻结并显示第一页而不是转到 home.php

这里是template/header.php:

<div class="container">
        <!--Head wrap starts-->
        <div id="head_wrap">
            <!--Header starts-->
            <div id="header">
                <img src="images/logo.png" style="float:left;"/>
                <form method="post" action="" id="form1">
                    <strong>Email:</strong>
                    <input type="email" id="email" name="email" placeholder="Email" required="required" />
                    <strong>Password:</strong>
                    <input type="password" id="pass" name="pass" placeholder="****" required="required"/>
                    <button type="submit" id="login">Login</button>
                </form>
            </div>
            <!--Header ends-->
        </div>

这里是login.php

<?php
        session_start();

        include("includes/connection.php");

        if(isset($_POST['login'])){

        $email = mysqli_real_escape_string($con,$_POST['email']);
        $pass = mysqli_real_escape_string($con,$_POST['pass']);

        $get_user = "select * from users where user_email='$email' AND user_pass='$pass'";

        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if($check==1){
                $email = mysqli_real_escape_string($con,$_POST['email']);

            $_SESSION['user_email']=$email;

                        echo "<script>window.open('home.php','_self')</script>";



        }
            else {
            echo "<script>alert('Passowrd or email is not correct!')</script>";
        }

        }
?>

请注意我已经试过了

        echo "<script> window.location.href = 'home.php';</script>";

而不是

        echo "<script>window.open('home.php','_self')</script>";

但仍然不起作用,因为它是教程,我通过 Whosebug 搜索找不到任何答案,感谢您的帮助。

您必须使用 header(...) 功能,但不要忘记您的页面最后保持 运行。不要忘记使用 with die 来停止你的脚本。 ;)

die(header("Location: home.php"))

或 5 秒后:

header("refresh: 5; url=home.php");

这是您的 HTML 代码,但带有提交按钮。你说所有文件都位于同一个文件夹中,所以这应该有效。我没有对 login.php 进行任何更改,但在提交页面时应该 运行。

<div class="container">
 <!--Head wrap starts-->
 <div id="head_wrap">
  <!--Header starts-->
  <div id="header">
   <img src="images/logo.png" style="float:left;"/>
   <form method="post" action="login.php" id="form1">
    <strong>Email:</strong>
    <input type="email" id="email" name="email" placeholder="Email" required="required" />
    <strong>Password:</strong>
    <input type="password" id="pass" name="pass" placeholder="****" required="required"/>
    <input type="submit" id="login" name="login" value="Login">
   </form>
  </div>
  <!--Header ends-->
 </div>
</div>

编辑: 我无法调试你的整个项目,但在查看了一些内容后我发现你没有使用 'name' 属性。提交页面时,将在 $_POST 数组中发送一个 name/value 对。如果您没有 'name' 属性,则不会发送任何内容。首先添加 'name' 属性。我已经修改了上面的 HTML 代码来告诉你如何。

 if($check==1){
            $email = mysqli_real_escape_string($con,$_POST['email']);

        $_SESSION['user_email']=$email;

                    return 1;
   }
        else {
         return 0;
    }

和 javascript 检查状态 1 和 0 然后 window.location.href 和 window.open 使用

检查你的文件..

1) header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

2) Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.

3) after header('location: home.php'); add exit();

4) after sesssion_start() add ob_start();