PHP标题(“Location:url.php”)无法在godaddy中运行

时间:2014-02-03 08:08:07

标签: javascript php html session header

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

还是因为SESSION?

还是因为UTF-8编码?

我确实将Dreamweaver中的页面属性从Western Europian更改为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());

?>

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

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

此外,正如@DainisAbols的评论中所述,您应该使用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();

答案 2 :(得分:0)

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