dongyuxiao6295 2014-07-05 00:15
浏览 38
已采纳

PHP - header('location:profile.php'); 不工作[重复]

This question already has an answer here:

when I hit login it keeps saying invalid login information. I want it to go to profile.php I used header('location:profile.php') I wonder whats wrong? may you guys take a look please, and let me know if I'm missing something or had typos. thanks!

    <?php
//database information
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

//connect to database
mysql_connect($host, $user, $pass);
mysql_select_db($db);

//select table
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1";
    $res = mysql_query($sql);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin Login</title>
<link href="../sources/stylesheet.css" rel="stylesheet" type="text/css" />
</head><body>
<div id="content_login">
  <div id="header">
    <p align="center"><u>NJROTC ADMINS</u></p>
    <div id="Quote">
      <p align="center">You cannot be on this page if you weren't given the login information!  </p>
    </div>
  </div>
  <p>&nbsp;</p>
  <p><br />
  </p>
  <form id="form1" name="form1" method="post" action="/njrotc/pages/login.php">
    <table width="331" height="141" border="0">
      <tr>
        <td width="112">Username</td>
        <td width="178"><input type="text" name="username" id="text2" /></td>
      </tr>
      <tr>
        <td>Password</td>
        <td><input type="password" name="password" id="password" /></td>
      </tr>
      <tr>
        <td colspan="2"><div class="php">
          <?php
if (isset($_POST['username'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    if (mysql_num_rows($res) == 1) {
    echo "You have successfully logged in.";
    exit();
    } else {
    echo "Invalid log in information.";
    exit();
        header ('location:profile.php');
    }
}
?>
        </div></td>
      </tr>
      <tr>
        <td><input type="submit" name="submit" id="submit" value="Log In" /></td>
        <td>&nbsp;</td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>
</div>
  • 写回答

1条回答 默认 最新

  • duanpu4143 2014-07-05 00:21
    关注

    Here is your code:

    echo "Invalid log in information.";
    exit();
        header ('location:profile.php');
    

    First when you execute an echo the server sends headers to the browser, so you can’t have that before a header call. But past any of that you have an exit(); before the header ('location:profile.php'); call. So that will never execute it. Just do this instead:

    // echo "Invalid log in information.";
    // exit();
    header ('location:profile.php');
    

    Commenting out the echo and exit just in case there is a valid reason for you to have those there. Perhaps for debugging? But if this is in production, those are not needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集