绝对无法弄清楚标题问题

时间:2013-08-02 03:05:15

标签: php mysql header

我确实一整天都试图解决这个问题。

以下是我的一系列事件:

我有一堆.php页面,我希望我的网站标题位于它们的顶部。很自然地,我包括header.php

但是,由于我的header.php文件在第81行包含HTML,并且我想在提交表单后进行重定向,因此在发送所有标题之前,我不能包含我的header.php。

所以我开始并收到此错误消息:

Warning: Cannot modify header information - headers already sent by (output started at header.php:81) in submit_build.php on line 47

很自然地,我动了我的

include header.php;

在页面的下方,在所有HTML开始时位于上面的位置。

我这样做,并收到此错误消息:

Notice: Undefined variable: _SESSION in /home6/warfram2/public_html/submit_build.php on line 49

我的所有其他文件都有提交表单,header.php文件,并且需要重定向工作。

由于某种原因,这个ONE文件不起作用。

这是我的header.php文件的代码:

<?php //header.php
session_start();

include_once 'login_users.php';

if (isset($_SESSION['username']))
{
    $username = $_SESSION['username'];
    $loggedin = TRUE;
}
else $loggedin = FALSE;

if ($loggedin == TRUE)
{

$usr = urlencode($username);

    echo <<<_END
.... HTML down here

我已经阅读了标题上的所有stackoverflow线程,包括以下内容:

How to fix "Headers already sent" error in PHP PHP - Cannot modify header information Headers Already Sent, cannot find issue

我偶尔也会收到错误:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

我的网站流程是:

登录[只是一段代码]:

if (!empty($_POST['username']) &&
    (!empty($_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_start();
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $pw_temp;
            $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR'];
            header('Location: index.php');
            exit;
     }      
}else{
include_once 'header.php';

然后你被重定向到索引。当你想访问这个页面时,我遇到了很多困难,包括所有PHP负载,并且包含HEADER.PHP是最后要加载的东西。

在PHP内部绝对没有回声,打印或其他任何东西。我的php标签之前没有空格,没有BOM字符,没有。

这是我这个令人沮丧的页面的前几行的代码:

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


$db_server = mysql_connect($db_hostname, $db_username, $db_password);

mysql_select_db($db_database)
    or die("Unable to select database: " . mysql_error());

$choice = $_GET['choice'];


if (isset($_POST['buildname']) &&
    isset($_POST['weapon']) &&
    isset($_POST['mod1']) &&
    isset($_POST['description']) &&
    isset($_POST['category']) &&
    isset($_POST['hidden']) &&
    isset($_POST['password']))
{
$buildname = clean(sanitizeString($_POST['buildname']));
$buildurl = urlencode($buildname);
$weapon =   sanitizeString($_POST['weapon']);
$modcap = sanitizeString($_POST['modcap']);
$section = $choice;
$mod1 =     sanitizeString($_POST['mod1']);
$mod2 =     sanitizeString($_POST['mod2']);
$mod3 =     sanitizeString($_POST['mod3']);
$mod4 =     sanitizeString($_POST['mod4']);
$mod5 =     sanitizeString($_POST['mod5']);
$mod6 =     sanitizeString($_POST['mod6']);
$mod7 =     sanitizeString($_POST['mod7']);
$mod8 =     sanitizeString($_POST['mod8']);
$polarity1 =    sanitizeString($_POST['polarity1']);
$polarity2 =    sanitizeString($_POST['polarity2']);
$polarity3 =    sanitizeString($_POST['polarity3']);
$polarity4 =    sanitizeString($_POST['polarity4']);
$polarity5 =    sanitizeString($_POST['polarity5']);
$polarity6 =    sanitizeString($_POST['polarity6']);
$polarity7 =    sanitizeString($_POST['polarity7']);
$polarity8 =    sanitizeString($_POST['polarity8']);
$description =  sanitizeString($_POST['description']);
$category =     sanitizeString($_POST['category']);
$hidden =       sanitizeString($_POST['hidden']);
$pw_check =     sanitizeString($_POST['password']);

$pw_check = md5($pw_check);
if ($pw_check == $_SESSION['password'])
{
header("Location: account.php");
$add_build = "INSERT INTO weapons VALUES(NULL,'$username', '$buildname', '$section', '$weapon', '$modcap', '$mod1', '$mod2', '$mod3', '$mod4', '$mod5', '$mod6', '$mod7', '$mod8', '$polarity1', '$polarity2', '$polarity3', '$polarity4', '$polarity5', '$polarity6', '$polarity7', '$polarity8', '$category', '$hidden', '$description', NULL, '{$_SESSION['ipaddress']}', '$buildurl')";
mysql_query($add_build);

exit;
}
else{
die("Incorrect password.");


}
}

//Set Dropdown Menu HTML Variables

如果我决定加入一个函数,如;

if(isset($_SESSION['username']))
{
code here
}else{die("Not logged in");}

if(!empty($_SESSION['username']))
{
code here
}else{die("Not logged in");}

要验证用户是否已登录,即使我的网站上的其他任何页面都显示已登录,我也会立即收到此消息。

我和我的网站有什么问题? 我觉得如果没有专业人士的帮助,我永远无法解决这个问题。

感谢您的帮助。

如果您认为自己可能知道错误,我会提供任何其他代码。

干杯

2 个答案:

答案 0 :(得分:0)

因为我以前遇到过这个问题,所以完全脱离了我的头脑,但仔细检查你的包含文件的空白区域。这可能是你的php标签之前或之后的空格或换行符。如果这些文件中有空格,则会将其发送到浏览器,此时您无法设置会话或标题。

答案 1 :(得分:0)

使用ob_start();和ob_end_flush();围绕我的PHP。

我完全不知道输出数据是什么。

也许$ _GET数组的东西。仍然没有意义,我会得到其他错误,如未定义的变量_SESSION ....

无论如何......我觉得这是我的计划的临时修复,但我现在就接受它。

干杯。