-2

伙计们,我对 php 很陌生,我正在尝试制作一个登录页面。

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

我试过 iwth 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
        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>

请问有人可以帮助我吗?谢谢

4

3 回答 3

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>
于 2012-04-29T12:11:01.000 回答
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']));
于 2012-04-29T13:49:04.667 回答
-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(); ?>
于 2012-04-29T12:11:28.300 回答