如何直接将php发送到另一个页面


How to directly send php to another page?

我是编程新手,正在使用html、mySQL和PHP尝试为用户配置文件设置用户名和密码。然后我想向用户显示一个自定义页面。

问题是,在我检查它们是否在数据库中后,我不知道如何将它们直接发送到新页面

我试过使用我在谷歌上找到的一个名为header()的函数,但我一直遇到错误。在使用header()函数之前,我试图通过确保没有输出到屏幕来解决这个问题,现在我很确定我没有。我实际上不知道问题出在哪里,我正试图找到另一种方法来解决这个问题,或者只是用header()找到答案。无论如何,这是我的代码:

<?php
require "connect_to_mysql_and_test_connection.php";
//use the variable $db to access the database


?>
<html>
<head>
            <title>User Login</title>
            <h2>Login:</h2>
</head>
<body>
    <!--this makes a form to take the user input. the input of username and password. the submit buttons value is always submitted to $_POST-->
    <form method="post">
            Create user name:<br>
            <input type="text" name="username" /><br>
            Create password:<br>
            <input type="text" name="password"/><br>
            <input type="submit" name="submit"/><br>
    </form>
    <?php
            $compare_username="";
            $compare_password="";
            //get the username and password fields from the database and store it in a variable
            $username_password_fields=$db->query("SELECT username, password FROM users");//mysql object
            //get number of rows
            $rows= $username_password_fields->num_rows;
            //create a while loop to go through each row.
            while($rows >=1){
                            //iterate through each array returned by fetch_assoc to see if the username already exists
                            foreach($username_password_fields->fetch_assoc() as $key=>$value){
                                    //echo $key . $value . "<br>";
                                    //************check to see if the username exists****************
                                    if ($value === $_POST['username']){
                                            $compare_username=$value;
                                    }
                                    else  if ($value === $_POST['password']){
                                            $compare_password=$value;
                                    }

                            }
                            //check to see if the username matches the password
                            $rows -= 1;//decrement the rows remaining
                            if ($compare_username != "" and $compare_password != ""){
                                            //write code to send them to their custom page
                                            **header("Location:http://jasongriffore.atwebpages.com/myfirstworkingsite.php")<--This is the problem. please help!**
                                            echo "Ding ding ding a match!";
                                    return;
                            }
                            else if ($compare_username != ""){
                                    echo "This user name exists but the password is incorrect";
                                    //they can try again on the same page
                                    return;
                            }

            }
            //if after running the while loop they didn't get sent to their custom page, the must be new!
            echo "Let's create a new account for you!";
            //enter code to add a new user to the database 
    echo print_r($_POST, true);        
    ?>


使用header()函数:

header('Location: mypage.php');

在你这样做之前,请确保没有输出发送到浏览器,正如我所看到的,因为你的HTML <form>在你的PHP处理之前是。改变这一点。

我建议使用ob_start()ob_end_flush()来缓冲浏览器的输出。你可以在这里阅读更多关于这方面的内容。您想要这样做的主要原因是,header标记要求尚未向浏览器发送任何输出。通过回显<html><head>。。。它已经将数据发送到客户端,因此无法再发送头文件(毕竟它们是头文件,用于引导输出。)ob_start()将缓冲(或存储)所有内容,直到您准备好输出它,并允许您稍后发送头文件,因为输出尚未发送到浏览器。

<?php
require "connect_to_mysql_and_test_connection.php";
//use the variable $db to access the database
//Buffer the output
ob_start();

?>
<html>
<head>
            <title>User Login</title>
            <h2>Login:</h2>
</head>
<body>
    <!--this makes a form to take the user input. the input of username and password. the submit buttons value is always submitted to $_POST-->
    <form method="post">
            Create user name:<br>
            <input type="text" name="username" /><br>
            Create password:<br>
            <input type="text" name="password"/><br>
            <input type="submit" name="submit"/><br>
    </form>
    <?php
            $compare_username="";
            $compare_password="";
            //get the username and password fields from the database and store it in a variable
            $username_password_fields=$db->query("SELECT username, password FROM users");//mysql object
            //get number of rows
            $rows= $username_password_fields->num_rows;
            //create a while loop to go through each row.
            while($rows >=1){
                            //iterate through each array returned by fetch_assoc to see if the username already exists
                            foreach($username_password_fields->fetch_assoc() as $key=>$value){
                                    //echo $key . $value . "<br>";
                                    //************check to see if the username exists****************
                                    if ($value === $_POST['username']){
                                            $compare_username=$value;
                                    }
                                    else  if ($value === $_POST['password']){
                                            $compare_password=$value;
                                    }

                            }
                            //check to see if the username matches the password
                            $rows -= 1;//decrement the rows remaining
                            if ($compare_username != "" and $compare_password != ""){
                                    //write code to send them to their custom page
                                    header("Location:http://jasongriffore.atwebpages.com/myfirstworkingsite.php");
                                    return;
                            }
                            else if ($compare_username != ""){
                                    echo "This user name exists but the password is incorrect";
                                    //they can try again on the same page
                                    return;
                            }

            }
            //if after running the while loop they didn't get sent to their custom page, the must be new!
            echo "Let's create a new account for you!";
            //enter code to add a new user to the database 
    echo print_r($_POST, true);      
    //Write the output to the browser
    ob_end_flush();  
    ?>

使用javascript:

<script>window.location = 'http://jasongriffore.atwebpages.com/myfirstworkingsite.php';</script>