会话启动并且无法修改标头

时间:2014-11-20 15:41:07

标签: php session

我在错误日志中遇到此问题:

PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/site/public_html/index.php:1) in /home/site/public_html/controller/controller.php on line 5   <---   and this on admin panel error log --->  PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/site/public_html/config.php:1) in /home/site/public_html/functions/functions.php on line 24

PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/site/public_html/config.php:1) in /home/site/public_html/admin/auth/index.php on line 8

所有在我的本地服务器上工作正常,但在托管我不能使用管理面板正常。我必须在浏览器上键入完整路径才能连接,并且我可以在管理员上更改某些内容我必须再次访问它。对不起我的英语不好,如果有人请求我帮忙。

<?php
define('site', TRUE);
session_start();
include $_SERVER['DOCUMENT_ROOT'].'/config.php';

if($_SESSION['auth']['admin']){
header("Location: ../");
exit;
}

if($_POST){
$login = trim(mysql_real_escape_string($_POST['user']));
$pass = trim($_POST['pass']);
$query = "SELECT .......................... = '$login' AND id_role = 2 LIMIT 1";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
if($row['password'] == md5($pass)){
$_SESSION['auth']['admin'] = htmlspecialchars($row['name']);
$_SESSION['auth']['user_id'] = $row['customer_id'];
header("Location: ../");
exit;
}else{
$_SESSION['res'] = '<div class="error">wrong pass!</div>';
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
}

?>
<!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" />
<link rel="stylesheet" type="text/css" href="../<?=ADMIN_TEMPLATE?>style.css" />
<title>admin</title>
</head>

<body>
<div class="karkas">
<div class="head">
    <a href="#"><img src="../<?=ADMIN_TEMPLATE?>images/logoAdm.jpg" /></a>
    <p>log admin</p>
</div>
<div class="enter">
<?php 
if(isset($_SESSION['res'])){
echo $_SESSION['res'];
unset($_SESSION['res']);
}
?>
<form method="post" action="">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Username:</td>
<td><input type="text" name="user" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="image" src="../<?=ADMIN_TEMPLATE?>images/enter_btn.jpg" name="submit" /></td>
</tr>
</table>      
</form>
</div>
</div>
</body>
</html>

CONFIG.PHP

<?php

defined('site') or die('Access denied');
define('PATH', 'http://site');
define('MODEL', 'model/model.php');
define('CONTROLLER', 'controller/controller.php');
define('VIEW', 'views/');
define('TEMPLATE', PATH.VIEW.'site/');
define('PRODUCTIMG', PATH.'userfiles/product_img/baseimg/');
define('GALLERYIMG', PATH.'userfiles/product_img/');
define('SIZE', 1048576);
define('HOST', 'localhost');
define('USER', 'sdfsdfsdfsd');
define('PASS', 'sdfsdfsdfsd');
define('DB', 'sdfsdfsdfsd');
define('TITLE', 'სახლი რომელიც შენია!');
define('ADMIN_EMAIL', 'admin@site.com');
define('PERPAGE', 9);
define('ADMIN_TEMPLATE', 'templates/');
mysql_connect(HOST, USER, PASS) or die('No connect to Server');
mysql_select_db(DB) or die('No connect to DB');
mysql_query("SET NAMES 'UTF8'") or die('Cant set charset');

AUTH索引文件

<?php
define('site', TRUE);
session_start();

include $_SERVER['DOCUMENT_ROOT'].'/config.php';

if(!$_SESSION['auth']['admin']){
header("Location: " .PATH. "admin/auth/enter.php");
exit;
}else{
header("Location: " .PATH. "admin/");
exit;
}

1 个答案:

答案 0 :(得分:-1)

在开始时尝试ob_start();

这将缓冲输出直到脚本结束,并且在大多数情况下,您将无法获得无法修改标头信息警告。 更多信息here。 您的代码应该是这样的(HTTP请求的第一个文件):

<?php
ob_start();
session_start();
define('site', TRUE);
include $_SERVER['DOCUMENT_ROOT'].'/config.php';
...

在您的代码中,警告似乎是由您的(headerdie)尝试创建的,因此应该在之前启用缓冲。

另外,因为警告在第一行,意味着输出应该在那里发生,并且假设你没有任何额外的空格和缩进,请检查文件的编码(index.php,config。 php)并将其保存为 UTF-8,不含BOM