【问题标题】:PHP header ("Location: url.php") not working in godaddyPHP标头(“位置:url.php”)在godaddy中不起作用
【发布时间】:2014-02-03 08:08:07
【问题描述】:

美好的一天。我有一个关于标题的问题(“位置:url.php”);在 PHP 中。我使用 Macromedia Dreamweaver MX 2004 编写了代码。所有标头在 localhost 服务器中都可以正常工作,但是当上传到 godaddy 服务器时,登录后重定向卡在(login.php)页面中,没有错误。在这里,我提供了文件(表单和 PHP 文件),真的希望所有对这个特定问题有疑虑和知识的人都可以帮助我。我真的不知道问题的原因是什么。是因为代码吗?

还是因为 SESSION?

还是因为 UTF-8 编码?

我确实将 Dreamweaver 中的页面属性从西欧更改为 UTF-8,但未检查 BOM 签名。非常感谢您的帮助。提前致谢。

这是在 (adminlogin.php)

<?php
session_start();
include('conn.php');
if(isset($_SESSION['username']) && isset($_SESSION['password']))
{ header("Location: admin.php");} 
?>
<html>
<head>
<title>Admin Login</title>
<style>
#popUpYes
{
    width: 60px;
    height: 25px;
    background-color: white;
}
#popUpYes:hover
{
    background-color: #D4A017;
} 
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h2><font color=gold> Admin Login </font></h2>
<form method = "post" action = "login.php" >
<table>
<tr>
    <td> <font color=silver>Admin ID</font></td>
    <td>:</td>
    <td><input type="text" name="username" size=20 ><br></td></tr>
    <tr>
    <td> <font color=silver>Password </font></td>
    <td>:</td>
    <td><input type="password" name="password" size=20><br></td>
</tr>
<tr>
</table>
<input type = "hidden" name = "submit">
<input type = "submit" name = "submit" value = "login" id="popUpYes" >
<input type = "reset" name = "reset" value = "reset" id="popUpYes" >
</form>
<body background="c.png" bgproperties="fixed">
</body>
</html>

这是在 (login.php)

<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<?php
session_start();
include('conn.php');
if($_REQUEST['username']=="admin" && $_REQUEST['password']=="abc123")
{
    $_SESSION['username'] = "admin";
    $_SESSION['password'] = "abc123";
    header("Location:admin.php");
}
else{ ?> <script> 
    alert ( "Wrong combination of Username and Password. Please try again.");
    </script> <?php
    header("Location:adminlogin.php");
}
?>
<body background="c.png" bgproperties="fixed">
</body>
</html>

这是在 (admin.php)

<?php
session_start();
include('conn.php');
if(!isset($_SESSION['username']) || !isset($_SESSION['password']))
{ header("Location:adminlogin.php"); }
?>
<html>
<head>
<style type="text/css">
a.button:hover
{
    background: #D4A017;
} 
</style>
<style> 
#pop
{
    width: 60px;
    height: 25px;
    background-color: white;
}
#pop:hover
{
    background-color: #D4A017;
}  
</style>
<title>Admin Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method = "post" action = "verification.php">
<br>
<h2><font color=gold>Admin Options: </h2><br></font>
<font color=white><b>
<input type ="radio" name="user" value="register">Click Here To Register An Agent<br>
<input type ="radio" name="user" value="regsales">Click Here To Create New Sales Record<br>
<input type ="radio" name="user" value="view">Click Here To View Agent Details and Sales<br>
<input type ="radio" name="user" value="edit">Click Here To Edit Agent Details and Sales<br></b>
<br><br>
<input type = "hidden" name = "verify"><br>
<input type="submit" name="submit" value="submit" id="pop">
<input type="reset" name="reset" value="Clear" id="pop"><br>
</font>
<body background="c.png" bgproperties="fixed"> 
<button><a class="button" href="logout.php" >Logout</button></a>
</body>
</html>

这是在 (conn.php)

<?php

$link = mysqli_connect('domain', 'username', 'password','databaseName') or
 die(mysqli_error());

?>

【问题讨论】:

  • header("Location:adminlogin.php") 错误的header("Location:admin/login.php") 写入
  • 您真的需要发布所有代码吗?您能否以正确的格式发布它?不要加倍空格,将整个代码块标记为代码,而不是每一行。
  • 嗨@Vickey,我不确定我是否理解你。这会导致它转到(admin/login.php)而不是我想要的页面(adminlogin.php)吗?
  • 对不起@Barmar。这是我问的第一个问题。我稍后会编辑它。
  • @Vickey 错了。除了用户提供的内容之外,您不知道用户的树结构。

标签: javascript php html session header


【解决方案1】:

我也有这个问题。只需在脚本顶部使用 ob_start(); 即可获得多个标题。

ob_start() 函数将所有服务器输出缓冲到一个字符串变量。这允许您在代码中的任何位置操作 HTTP 标头。

【讨论】:

  • ob_start(); 是做什么的?你能扩展你的答案吗?
  • @evolutionxbox 此函数将所有服务器输出缓冲到字符串变量。这允许您在代码中的任何位置操作 HTTP 标头。
  • 这是真正的解决方案。我的登录脚本可以在 localhost 上运行,但不能在 GoDaddy 上运行,即使我没有在标头之前发送任何输出。奇怪,但好在你的解决方案有效。
【解决方案2】:

嗯,据我所知,这里的答案很简单;

如the manual page for header() 所述,您不能在函数调用之前发送任何内容。删除 php 块上方的 html,并将其移动到其他位置,您的代码应该可以工作。

此外,正如 @DainisAbols 在 cmets 中所述,您应该使用 die() 或 exit()(它们都做同样的事情)来防止进一步的脚本执行。

编辑:

在您的 login.php 页面中,您有以下代码:

<html>

<body> <!-- 
            Right here, you have content, above your header() call.  Because this
            content exists, the headers for the page have already been sent, and 
            thus, they cannot be sent again by header().  You need to move this
            block of html to after your php script, so that the first line in
            your file is the opening php tag: (<?php), with nothing before it
       -->

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>

<?php

session_start();

【讨论】:

  • 作为补充说明,考虑到上述情况,我很难相信函数调用在您的本地计算机上正常工作。
  • 感谢您的回答。所以在html代码中集成php不是一个好习惯吗?你是什​​么意思把它移到别的地方?我现在真的很迷茫。 :-|
  • 我不知道它为什么会起作用,但如果你想要确凿的证据,我可以用图片向你保证@Daedalus :)
  • 非常感谢@daedalus。对此,我真的非常感激。 :)
  • @Aliff 添加了我希望是彻底的解释。
【解决方案3】:

您没有在登录时进行重定向,因为在尝试发送 login.php 中的位置标头之前,您的标头已作为呈现 html 的结果发送。将文件顶部的标记移到 else 子句中,这样就可以解决问题(假设没有任何其他潜在问题)。

【讨论】:

  • 谢谢你,布兰登。现在我真的明白了这个麻烦的原因了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2016-08-21
  • 1970-01-01
相关资源
最近更新 更多