如何使用PHP重定向到另一个页面 [英] How to redirect to another page using PHP

查看:101
本文介绍了如何使用PHP重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个包含登录页面的网站.成功登录后,我需要将用户重定向到他们的个人资料页面,但是我不知道如何在PHP中做到这一点(这是我的第一个站点).

I am building a website which includes a login page. I need to redirect the user to their profile page once they've logged in successfully, but I don't know how to do that in PHP (It's my first site).

我已经搜索了Internet,并被告知header()函数应该可以解决问题,但是只有在使用前我没有输出任何信息的情况下,它才有效.

I've searched the internet and have been told that the header() function should do the trick, but it will only work if I haven't outputted any information before using it.

就是这个问题.我已经输出了很多信息(包括用于构建登录页面本身的HTML).

That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself).

那我如何将用户从一个页面重定向到下一个页面?

So how do I redirect the user from one page to the next?

我有什么选择?另外,在这些情况下的最佳做法是什么?

What options do I have? Also, what is the best practice in these instances?

这是我的整个login.php页面

Here's my entire login.php page

<?php 

session_start(); 

echo "<!DOCTYPE html> 
  <html> 
     <head> 
        <meta charset='utf-8'> 
        <title>Sprout</title>
    <link rel='stylesheet' href='stylesheet.css' type='text/css'>
     </head>
 <body>
    <div class='box'>
    <form action='login.php' method='post'>
       Name<br /> <input type='text' name='username' class='form'/><br />
       Password<br /> <input type='password' name='password' class='form'/>
       <input type='submit' value='Login' class='button' />
    </form>
    </div>
 </body>
  </html>";

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    $username = $_POST["username"];
    $password = $_POST["password"];

    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "root";

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");

    $dbname = "database";

    mysql_select_db($dbname);

    $query = "SELECT username FROM users WHERE username = '$username' AND password = '$password'";

    $result = mysql_query($query) or die ("Failed Query of " . $query);


    while($row = mysql_fetch_assoc($result))
    {
            $_SESSION["user"] = $username;
    }
}
?>

推荐答案

就是这个问题.我已经输出了很多信息(包括用于构建登录页面本身的HTML).那么,如何将用户从一个页面重定向到下一个页面?

That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself). So how do I redirect the user from one page to the next?

这意味着您的应用程序设计已损坏.业务逻辑正在运行时,您不应该进行输出.使用模板引擎(例如 Smarty )或使用

This means your application design is pretty broken. You shouldn't be doing output while your business logic is running. Go an use a template engine (like Smarty) or quickfix it by using output buffering).

另一种选择(虽然不是一个好选择!)将输出JavaScript以进行重定向:

Another option (not a good one though!) would be outputting JavaScript to redirect:

<script type="text/javascript">location.href = 'newurl';</script>

这篇关于如何使用PHP重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆