6

我确定这个问题之前已经被问过,但我已经彻底寻找答案但无济于事。(我见过的唯一答案涉及 ajax)但我只使用 javascript、PHP 和 HTML。

我有一个 login.php 页面,并且我已经创建了一个 HTML 页面,该页面将在用户成功登录后立即成为登录页面。我该怎么做?

下面是我的登录页面的代码,登录后的登陆页面叫做transfer.html:

登录.PHP

  <div id="content">
     <h3>Login to Internet Banking</h3>
     <form id="login" action="" method="post">
        <p>
          <label for="userid">UserID:</label>
          <input type="text" name="UserID" id="UserID"/>
        </p>
        <p>
          <label for="PIN">PIN:</label>
          <input type="password" name="PIN" id="PIN" />
        </p>

        <p>
          <input type="submit" name="btnSend" value="Login" class="submit_button" />

        </p>
      </form>
      <td>&nbsp;</td>
 <p>
        Not yet registered?
 <a href="registration.php">Click here to register</a>
 </p>

  <div id="wrap">
        <!-- start PHP code -->
        <?php

            mysql_connect("localhost", "root", "") or die(mysql_error()); // Connect to database server(localhost) with UserID and PIN.
            mysql_select_db("registrations") or die(mysql_error()); // Select registration database.

            if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['PIN']) && !empty($_POST['PIN'])){
                $UserID = mysql_escape_string($_POST['name']);
                $PIN = mysql_escape_string(md5($_POST['PIN']));

                $search = mysql_query("SELECT UserID, PIN, active FROM users WHERE UserID='".$UserID."' AND PIN='".$PIN."' AND active='1'") or die(mysql_error()); 
                $match  = mysql_num_rows($search);

                if($match > 0){
                    $msg = 'Login Complete! Thanks';
                }else{
                    $msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
                }
            }


        ?>
        <!-- stop PHP Code -->
        <?php 
            if(isset($msg)){ // Check if $msg is not empty
                echo '<div class="statusmsg">'.$msg.'</div>'; // Display our message and add a div around it with the class statusmsg
            } ?>

    </div>
        </div>
4

7 回答 7

21

首先,将所有 PHP 代码移到顶部。没有它,我下面的代码将无法工作。

要进行重定向,请使用:

header('Location: http://www.example.com/');

另外,请考虑我的建议。由于这不是今天的第一个问题,而且您的所有问题都与基础知识有关,因此您应该考虑阅读一些好的 PHP 书籍以了解其工作原理。

在这里您可以找到免费书籍的有用链接: https ://stackoverflow.com/tags/php/info

于 2013-02-26T13:53:34.833 回答
11

使用 php 代码生成的 Javascript 重定向:

 if($match > 0){
     $msg = 'Login Complete! Thanks';
     echo "<script> window.location.assign('index.php'); </script>";
 }
 else{
     $msg = 'Login Failed!<br /> Please make sure that you enter the correct  details and that you have activated your account.';
 }

仅限 PHP 重定向:

<?php
    header("Location: index.php"); 
    exit;
?>
于 2013-02-26T13:54:46.013 回答
0

试试 header("Location:home.php"); 而不是显示 $msg = '登录完成!谢谢'; 希望它会帮助你。

于 2013-02-26T13:53:15.297 回答
0

您需要location按如下方式设置标题:

header('Location: http://www.example.com/');

当然替换http://www.example.com为您的登录页面的网址。

在您完成用户登录的地方添加此内容。

注意:重定向用户时将立即重定向,因此您的消息可能不会显示。

此外,您需要将 PHP 代码移动到文件顶部才能使此解决方案正常工作。

另一方面,请参阅我上面关于使用已弃用mysql语句的评论,并考虑使用更新、更安全和改进的mysqliorPDO语句。

于 2013-02-26T13:53:17.840 回答
0

可以这样使用

if($match > 0){
 $msg = 'Login Complete! Thanks';
 echo "<a href='".$link_address."'>link</a>";
 }
else{
 $msg = 'Login Failed!<br /> Please make sure that you enter the correct  details and that you have activated your account.';
}
于 2017-10-05T06:45:25.640 回答
-1

您还可以在登录后提供指向页面的链接,并在 10 秒后使用 javascript 自动重定向。

于 2014-06-17T15:27:18.773 回答
-2

只需在您使用 PHP 代码提供的最后一条消息之后添加以下代码

打印'window.location.assign("index.php")

于 2016-08-12T13:20:49.043 回答