php 登录成功后重定向到新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15090785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:33:19  来源:igfitidea点击:

Redirecting to a new page after successful login

phphtmlformslogin

提问by Kaos

I'm sure this question has been asked before but I have searched thoroughly for an answer but to no avail. (The only answers I've seen involve ajax) But I'm using just javascript, PHP and HTML.

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

I have a login.php page and I have already created a HTML page which is to be the landing page right after a user is successfully logged in. How do I go about this?

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

The following is my code for the login page and the landing page after the login is called transfer.html:

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

LOGIN.PHP

登录.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>

回答by Serge Kuharev

First of all, move all your PHP code to the top. Without it, my code below wont work.

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

To do the redirect, use:

要进行重定向,请使用:

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

Also, please consider my advice. Since it's not the first question today and all your questions are related to basics, you should consider reading some good PHP book to understand how things work.

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

Here you can find useful links to free books: https://stackoverflow.com/tags/php/info

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

回答by Hackerman

Javascript redirection generated with php code:

使用 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 redirection only:

仅限 PHP 重定向:

<?php
    header("Location: index.php"); 
    exit;
?>

回答by Tapas Pal

Try header("Location:home.php"); instead of showing $msg = 'Login Complete! Thanks'; Hope it'll help you.

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

回答by War10ck

You need to set the locationheader as follows:

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

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

Replacing http://www.example.comof course with the url of your landing page.

http://www.example.com当然用你的登陆页面的 url替换。

Add this where you have finished logging the user in.

在您完成用户登录的地方添加它。

Note: When redirecting the user will be immediately redirected so you're message probably won't display.

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

Additionally, you need to move your PHP code to the top of the file for this solution to work.

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

On another side note, please see my comment above regarding the use of the deprecated mysqlstatements and consider using the newer, safer and improved mysqlior PDOstatements.

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

回答by vikash

May be use like this

可以这样使用

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

回答by user3727412

You could also provide a link to the page after login and have it auto redirect using javascript after 10 seconds.

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

回答by Kardam Trivedi

Just add the following code after the final message you give using PHP code

只需在您使用 PHP 代码给出的最终消息后添加以下代码

Print'window.location.assign("index.php")

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