如何在php中重定向到另一个页面

时间:2012-04-29 12:03:31

标签: php mysql

伙计我对php很新,我正在尝试创建一个登录页面。

如果声明为真,我如何重定向页面?

我尝试了iwth标题(“location:nextpage.php”);但它不起作用

<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>

        <?php
        mysql_connect("localhost", "root","******") or die(mysql_error());
        mysql_select_db("login") or die(mysql_error());

        if(isset($_POST['username']) && isset($_POST['password'])){
            $db_user = mysql_query("SELECT username FROM login_tb");
            $db_pass = mysql_query("SELECT password FROM login_tb");
            $ctrl_user = mysql_result($db_user,0);
            $ctrl_pass = mysql_result($db_pass,0);

            $username = $_POST['username'];
            $password = $_POST['password'];
            echo $username . $password;

            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }else{
                if($username == $ctrl_user && $password == $ctrl_pass){
                header("location: ./nextpage.php");
                }
            }
        }
        ?>
    </body>
</html>
请任何人都可以帮助我吗?感谢

3 个答案:

答案 0 :(得分:1)

标题就是这样,它出现在数据传输的开头。在尝试发送标题之前,您正在发送数据(大部分网页)。

将您的PHP代码移动到文件的顶部,然后在正确的位置回显另一个php块中的“错误的用户/通行证”

<?php
    mysql_connect("localhost", "root","******") or die(mysql_error());
    mysql_select_db("login") or die(mysql_error());

    if(isset($_POST['username']) && isset($_POST['password'])){
        $db_user = mysql_query("SELECT username FROM login_tb");
        $db_pass = mysql_query("SELECT password FROM login_tb");
        $ctrl_user = mysql_result($db_user,0);
        $ctrl_pass = mysql_result($db_pass,0);

        $username = $_POST['username'];
        $password = $_POST['password'];

        if(($username || $password) != NULL && $username == $ctrl_user && $password == $ctrl_pass){
            header("location: ./nextpage.php");
        }
    }
?>
<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>
        <?php
            echo $username . $password;
            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }
        ?>
    </body>
</html>

答案 1 :(得分:1)

有多种方法可以重定向页面,我喜欢这样做:

<?php
header('Location: /blah.php');
die('<meta http-equiv="refresh" content="0; url=http:/blah.php" />\n
<p>Please visit <a href="/blah.php">Blah.php</a>.</p>');
?>

如果标题失败,那么你就会有一个后备。

另外请允许我指出您应该清理数据输入以防止SQL注入/ XSS。 在这里:

$user = htmlspecialchars(mysql_real_escape_string($_POST['user']));

答案 2 :(得分:-1)

似乎输出缓冲问题,您可以使用ob_start()ob_end_fluh()

以下;

<? ob_start(); ?>
<html>
    <head>
        <title>Uploader</title>
    </head>
    <body>

        <form action="index.php" method="POST" enctype="application/x-www-form-urlencoded"> 
        <input id="login" class="username" type="text" name="username" autofocus placeholder="Username" maxlength="30"/>
        <input id="login" class="password" type="password" name="password" placeholder="Password" maxlength="15"/>
        <input id="login" class="submit" type="submit" value="Login" />
        <p>Last update <span id="lastupdate"><?php echo date('d-m-Y');?></span></p>

        <?php
        mysql_connect("localhost", "root","******") or die(mysql_error());
        mysql_select_db("login") or die(mysql_error());

        if(isset($_POST['username']) && isset($_POST['password'])){
            $db_user = mysql_query("SELECT username FROM login_tb");
            $db_pass = mysql_query("SELECT password FROM login_tb");
            $ctrl_user = mysql_result($db_user,0);
            $ctrl_pass = mysql_result($db_pass,0);

            $username = $_POST['username'];
            $password = $_POST['password'];
            echo $username . $password;

            if(($username || $password) == NULL){
                echo "you have entered wrong username or password! <br/> please contact site admin.";
            }else{
                if($username == $ctrl_user && $password == $ctrl_pass){
                header("Location: ./nextpage.php");
                }
            }
        }
        ?>
    </body>
</html>
<? ob_end_flush(); ?>