重定向到成功登录后的新页面(Redirecting to a new page after succ

2019-07-20 13:45发布

我敢肯定,这个问题以前有人问,但我已搜查彻底的答案,但无济于事。 (我见过的唯一的答案,包括AJAX),但我只使用了JavaScript,PHP和HTML。

我有一个页面的login.php,我已经创建了一个HTML页面,这是一个用户后,登陆页面右侧登录成功。我怎么去呢?

下面是我的登录页面和登录后的登录页面代码被称为transfer.html:

的login.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>

Answer 1:

首先,将你所有的PHP代码到顶部。 没有它,我下面的代码不会工作。

要做到重定向,使用方法:

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

另外,请考虑我的建议。 因为它不是今天第一个问题,您的所有问题都涉及到基础知识,你应该考虑读一些好的PHP的书,了解事情是如何工作的。

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



Answer 2:

用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;
?>


Answer 3:

尝试标题( “位置:home.php”); 而不是显示$味精=“登录完成! 谢谢'; 希望它会帮助你。



Answer 4:

您需要设置的location标题如下:

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

更换http://www.example.com与您的目标网页的URL课程。

增加这个,你已经完成登录用户。

注意 :当用户重定向将立即重定向所以你可能消息将不显示。

此外,你需要将你的PHP代码的文件,该解决方案正常工作的顶部。

在另一个方面说明,请参见上述关于使用过时的我的意见mysql声明,并考虑使用更新,更安全和改进mysqliPDO语句。



Answer 5:

可能是这样的使用

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.';
}


Answer 6:

你也可以提供一个链接,登录后的页面中,使用10秒后的JavaScript有它的自动重定向。



Answer 7:

只要你给出一个使用PHP代码的最后消息后添加以下代码

Print'window.location.assign( “的index.php”)



文章来源: Redirecting to a new page after successful login