0

I have this problem on error log:

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

All works fine on my local server but on hosting i cant use admin panel normal. i must type full path on browser for connect and whan i change somthing on admin i must path it again. sorry for my bad english if some1 understend me pls help.

<?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 index file

<?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;
}
lev
  • 1
  • 4

1 Answers1

-1

try ob_start(); in the beginning.

This will buffer the output until the end of the script and in most cases you will not get the Cannot modify header information warning. more info here. Your code should be like this (the first file you HTTP request):

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

In your code specifically, the warning seems to be created by your (header, die) attempts so buffering should be enabled before.

Also, because warnings are on the first line, meaning that output is supposed to happen there, and provided that you don't have any extra whitespaces and indentations there, check the encoding of the files (index.php, config.php) and save them as UTF-8 without BOM.

George
  • 310
  • 1
  • 9
  • @lev Just to clarify, the `ob_start();` should be at the first block of code (index.php) not the config.php. Put `session_start()` just below. – George Nov 20 '14 at 16:59
  • @lev And make sure that ` – George Nov 20 '14 at 17:06
  • i must put it Everywhere where i have session_start() ? – lev Nov 21 '14 at 09:43
  • @lev If you mean the `ob_start();`, you should put it at the beginning of the file that is requested first e.g. your index.php file. Note that this would work if server's PHP has output_buffering off... – George Nov 21 '14 at 10:13
  • @lev I edited the answer to reflect my proposal. Nothing else should be before the – George Nov 21 '14 at 10:44
  • i add index file of auth folder maybe its help u. what about ob.start() i put it but not help problem is same – lev Nov 21 '14 at 16:33
  • @lev The warnings are on index.php and config.php. It says that there is the output. I believe you should check the encoding of the file (index.php, config.php) and if it's UTF-8 with [BOM](http://en.wikipedia.org/wiki/Byte_order_mark) save it again with encoding UTF-8 without BOM – George Nov 21 '14 at 18:24
  • index file of auth folder was ANSI encoding i am using phpdesigner7 trying to set it UTF-8 but whan i save it and reopen encoding is ANSI again. i tryed to save it from text document its saved but with BOM. cant save with only UTF-8. – lev Nov 21 '14 at 18:57
  • whan i use it with utf-8 with bom on local server and connect to admin i get problem same what i have on hosting – lev Nov 21 '14 at 19:03
  • `code`Warning: Cannot modify header information - headers already sent by (output started at `code`Z:\home\site\www\admin\auth\index.php:1) in Z:\home\site\www\admin\auth\index.php on line 8 – lev Nov 21 '14 at 19:03
  • @lev ANSI won't have the same problem. UTF-8 with BOM is. I don't know about phpdesigner7 but you can use [Notepad++](http://notepad-plus-plus.org) and select Encoding -> Convert to UTF-8 without BOM from the menu easily. Do this to both index.php – George Nov 21 '14 at 19:10
  • ohh finaly all works fine thx alot. i think its wrong problem and many of people dont know this. just coding alot than try see ur work and getting this problem and many of same... its boring and rage... thx alot man... – lev Nov 21 '14 at 20:13
  • @lev how did you finally fix it? – George Nov 22 '14 at 07:01
  • i convert to UTF-8 W/O BOM index file of site and admin folder and auth folder. – lev Nov 22 '14 at 15:07
  • i use Notepad++. Otherwise, not changing with windows text document or phpdes7 – lev Nov 22 '14 at 15:09
  • i have question to u. my site on mvc struct. i have 1 category base and 1 product base. i want to have 2 category base and 2 product base different from each other. My question is how i can use functions of category 1 and products 1 to 2cat and 2produc. its possible? or i must dublicate that functions? – lev Nov 22 '14 at 15:31
  • @lev Sorry, I can't help you with that. You can ask a new question using mvc tag to ask for more help. I am glad that UTF-8 W/o BOM helped. – George Nov 22 '14 at 16:56