2013-07-31 30 views
1

在我的网站,在每个PHP页面的顶部,我有一个HTTP重定向/头......当调用它

include_once“的header.php”;

此文件包含HTML。

在我的文件'authenticate.php'中,我想在登录回索引后重定向。

我的代码如下: header('Location:http://www.URLHERE.com/index.php');

但是在提交后,页面只是刷新。它不重定向。重定向在我的本地主机开发服务器上正常工作,但一旦我在线上传,它就停止工作。

这是因为我的头包含HTML,它在header()函数之前调用?请注意,'header.php'文件中的所有HTML都位于HEREDOC标记中。

这里是我的代码:

<?php // login.php 
include_once 'header.php'; 
include_once 'functions.php'; 
require_once 'login_users.php'; 

$db_server = mysql_connect($db_hostname, $db_username, $db_password); 
if (!$db_server) die("Unable to connect to database:" . mysql_error()); 
mysql_select_db($db_database) 
    or die("Unable to find database:" . mysql_error()); 

if (isset($_POST['username']) && 
    isset($_POST['pw_temp'])) 
{ 
    $username = sanitizeString($_POST['username']); 
    $pw_temp = sanitizeString($_POST['pw_temp']); 
    $pw_temp = md5($pw_temp); 
    $query = "SELECT username,password FROM users WHERE username='$username' AND password='$pw_temp'"; 
    if (mysql_num_rows(mysql_query($query)) == 0) 
    { 
    die("Wrong info"); 
    } 
    else 
    { 
      $_SESSION['username'] = $username; 
      $_SESSION['password'] = $pw_temp; 
      $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR']; 
      header('Location: http://www.URLHERE.com/index.php'); 
     }  
} 

...more code down here 
+0

可能重复http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – deceze

回答

0

你可以做includeelse,从顶部文件中的行删除include_once 'header.php';一个像这样做:

if (isset($_POST['username']) && 
    isset($_POST['pw_temp'])) 
{ 
    $username = sanitizeString($_POST['username']); 
    $pw_temp = sanitizeString($_POST['pw_temp']); 
    $pw_temp = md5($pw_temp); 
    $query = "SELECT username,password FROM users WHERE username='$username' AND password='$pw_temp'"; 
    if (mysql_num_rows(mysql_query($query)) == 0) 
    { 
    die("Wrong info"); 
    } 
    else 
    { 
      $_SESSION['username'] = $username; 
      $_SESSION['password'] = $pw_temp; 
      $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR']; 
      header('Location: http://www.URLHERE.com/index.php'); 
     }  
} 
else 
{ 
include_once 'header.php'; 
//..... now your code!!!!! 

} 
[头已被送往PHP(的
+0

这很好用。非常感谢!我明白这是如何工作的,但现在,非常感谢你! 此致 – SteelyDan

0

在我的网站,在每个PHP页面的顶部,我有一个include_once 'header.php';

这是你在做什么错。

它有哪些是

<?php // login.php 
include_once 'functions.php'; 
require_once 'login_users.php'; 

// some code 

include 'output.php'; // ONLY HERE output starts. 

Here you can see a concise but complete example with some explanations and reasoning。但是摆脱你的header.php并开始使用模板的主要原因是你问的问题。

-1

也许是因为您在设置标题后执行的代码?在该行之后添加dieheader未停止执行!)。

相关问题