Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Pages: [1] 2

Author Topic: Cannot modify header information [SOLVED]  (Read 1331 times)

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Cannot modify header information [SOLVED]
« on: February 07, 2012, 12:01:46 PM »

This bug occur when login

Quote
[E_WARNING # 2]: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\snews-original\snews.php:3278)
 in C:\xampplite\htdocs\snews-original\snews.php on line 265

In default sNews171

line 3278 marked with red:
Quote
//NOTIFICATION
function notification($error = 0, $note = '', $link = '') {
   // adds a "Warning" option
   $title = $error == 0 ? l('operation_completed') : ($error !== 0? l('admin_error') : l('warning'));
   $note = (!$note || empty($note)) ? '' : '<p>'.$note.'</p>';
   switch(true){
      case (!$link):
         $goto = '';
         break;
      case ($link == 'home'):
         $goto = '<p><a href="'._SITE.'">'.l('backhome').'</a></p>';
         break;
      case ($link != 'home'):
         $goto = '<p><a href="'._SITE.$link.'/" title="'.$link.'">'.l('back').'</a></p>';
         break;
   }
   if ($error == 2) {
      $_SESSION[_SITE.'fatal'] = $note == '' ? '' : '<h3>'.$title.'</h3>'.$note.$goto;
      echo '<meta http-equiv="refresh" content="0; url='._SITE.$link.'/">';
      return;
   } else {
      $output = '<h3>'.$title.'</h3>'.$note.$goto;
      return $output;
   }
}

line 265 marked with red:
Quote
   if (!empty($MainQuery)){
      $Mainresult = mysql_query($MainQuery);
      if (mysql_num_rows($Mainresult) === 1 ){
         $R = mysql_fetch_assoc($Mainresult);
      } else if(!in_array($_GET['action'],explode(',',l('cat_listSEF')))){ // Patch #7 - 1.7.0
         $categorySEF = '404';
         header('HTTP/1.1 404 Not Found');
         unset($subcatSEF,$articleSEF); }
      update_articles();
   }

« Last Edit: February 11, 2012, 05:53:57 PM by Keyrocks »
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #1 on: February 07, 2012, 03:28:02 PM »

REVISED
This post has been revised to provide the cause of the problem and the solution for it.

WHAT CAUSES IT

In the //NOTIFICATION function, this section generates notification messages for type 2 errors, which includes META information to be included in the <head> of the template (index.php) file refreshes the page after logging in:
Code: [Select]
<?php

if (
$error == 2) {
$_SESSION[_SITE.'fatal'] = $note == '' '' '<h3>'.$title.'</h3>'.$note.$goto;
echo '<meta http-equiv="refresh" content="0; url='._SITE.$link.'/">';
return;
}

?>


The section of script in snews.php that checks the login form information when the user submits it uses notification strings with type 2 error in each of three condition states... and this is being parsed before  header('HTTP/1.1 404 Not Found'); is parsed. It must be parsed after header('HTTP/1.1 404 Not Found'); is parsed.

THE SOLUTION

So, the solution is to relocate the section of script that checks the login form information so that it is being parsed after header('HTTP/1.1 404 Not Found'); is parsed.

For sNews 1.7.1 last updated February 16, 2011: If you are using the sNews 1.7.1 package from the official download page marked as UPDATED: February 16, 2011... find this section of script in your snews.php file and delete it:
Code: [Select]
<?php

if(isset($_POST['Loginform']) && !_ADMIN) {
$user checkUserPass($_POST['uname']);
$pass checkUserPass($_POST['pass']);
unset($_POST['uname'],$_POST['pass']);
// Patch #18 - 1.7.1 - revised string by KikkoMax
if (checkMathCaptcha() && md5($user) === s('username') && md5($pass) === s('password')) {
//if (md5($user) === s('username') && md5($pass) === s('password') && checkMathCaptcha()) {
$_SESSION[_SITE.'Logged_In'] = token();
notification(2,'','administration');
} else { die( notification(2,l('err_Login'),'login')); }
}

if(
$_POST['submit_text'] && !_ADMIN){
die (notification(2,l('error_not_logged_in'),'home'));
}

?>


Then, copy this replacement section and paste it into your snews.php file just above //TITLE (which is where function title() starts).
NOTE: This section has been slightly modified to eliminate a PHP Warning Notice and to show the Home page once the login has been successfully completed.
Code: [Select]
<?php // DO NOT COPY THIS TAG

if(isset($_POST['Loginform']) && !_ADMIN) {
$user checkUserPass($_POST['uname']);
$pass checkUserPass($_POST['pass']);
unset($_POST['uname'],$_POST['pass']);
// Patch #18 - 1.7.1 - revised string by KikkoMax
if (checkMathCaptcha() && md5($user) === s('username') && md5($pass) === s('password')) {
//if (md5($user) === s('username') && md5($pass) === s('password') && checkMathCaptcha()) {
$_SESSION[_SITE.'Logged_In'] = token();
notification(2,'','administration');
} else { die( notification(2,l('err_Login'),'login')); }
}
if(isset(
$_POST['submit_text']) && !_ADMIN){   // Added: isset() to eliminate PHP Notice
die (notification(2,l('error_not_logged_in'),'home'));
}

// DO NOT COPY THIS TAG ?>


Two minor patches may also be required to eliminate the "Warning: Cannot modify header information" message.

Patch 1: Search snews.php for if (!empty($_GET['category'])) (which should be just below the string that starts with $l['ignored_items'])... and above it, paste in global $articleSEF;
This defines the $articleSEF variable as a global for use within the blocks of script below it.

Patch 2: Search snews.php for header('HTTP/1.1 404 Not Found'); and, in the line two lines above it, you should see $_GET['action'], which needs to have isset() wrapped around so that it looks like - else if(!in_array(isset($_GET['action']),

IMPORTANT NOTE:
This change will not been made to the Official sNews 1.7.1 download package until it has been reviewed and approved by the 'sNews Dudes'. When it is updated, the update date will also be changed from February 16.11, the last update date.
« Last Edit: February 14, 2012, 08:04:13 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information
« Reply #2 on: February 07, 2012, 11:52:41 PM »

Enable first
Quote
error_reporting(-1);

find in
//NOTIFICATION
Quote
echo '<meta http-equiv="refresh" content="0; url='._SITE.$link.'/">';

and change the content to 5 sec

after login, and you see the "bug"

You can try also
http://snewscms.com/forum/index.php/topic,10362.0.html
and see it in errors.log
« Last Edit: February 07, 2012, 11:59:17 PM by sibas »
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information
« Reply #3 on: February 08, 2012, 09:00:49 PM »

error_reporting is about how you have setup apache/php to display errors
I have install xdebug extension
http://www.xdebug.org/

I work with xampplite
PHP Version 5.3.1
MySQL version: 5.1.41 

sNews Version:   1.7.1
Issue Date:      October 22.10 (keyrocks)

Here is what I see (after enable to display errors)


index


login


after login


Of course is always a possibility to have mess with xampplite and not working as normal,
if I am the only one that have this problem then I install xampplite again  :)

Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information
« Reply #4 on: February 09, 2012, 11:37:29 PM »

I don't know if I can help but I put in center()
$action = "";

after the
Code: [Select]
global $categorySEF, $subcatSEF, $articleSEF;
$action = "";

seems like I get rid of the undefined variable $action

unfortunately Im not sure about this!  ::)
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information
« Reply #5 on: February 10, 2012, 02:37:55 AM »

I don't know if I can help but I put in center()
$action = "";

after the
Code: [Select]
global $categorySEF, $subcatSEF, $articleSEF;
$action = "";

seems like I get rid of the undefined variable $action - unfortunately Im not sure about this!  ::)

Function center() in the official (default) 1.7 download package I am working with does not have a globals string in it.
$action = ""; works. I used global $action; which also works too.
« Last Edit: February 11, 2012, 09:23:29 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information
« Reply #6 on: February 10, 2012, 05:02:09 PM »

Do you have see (in administration) when you try to add
Categories: Add new
Articles: Add new
Pages: Add new
Extra Contents: Add new

What about "Undefined variable: retrieve" ???

when you go to any categories have
Undefined variable: articleSEF
in line
Quote
   // Query for  / Category / subcategory / article /
   if ($articleSEF && substr( $articleSEF, 0, 2) != l('paginator') && substr( $articleSEF, 0, 2) != l('comment_pages')) {

Yes... there are NOTICES on a lot of other pages too... but yesterday I only managed to clean up the Home page first. I will continue on the other pages today. So far, adding isset() in some cases or just adding undefined variables to the global variables string at the top of a function (to define them globally) seems to eliminate the NOTICES.  :)
« Last Edit: February 10, 2012, 05:05:54 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #7 on: February 11, 2012, 05:27:21 PM »

ATTENTION - Sibas and anyone else watching this topic thread...

I've found the problem causing the PHP "Cannot modify header information" Notice message Sibas discovered, and have posted the cause and solution in the second post )my first) of this topic thread (near the top of thie first page). I have also marked this topic thread as [SOLVED] so that anyone else looking for the solution can find it.

I've solved all of the other PHP Warning Notices and have posted all of the patches in one place in the 1.7 Patches/Fixes Board and deleted the related posts Sibas and I had made in this thread to eliminate any confusion.
« Last Edit: February 11, 2012, 09:28:26 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information [SOLVED]
« Reply #8 on: February 11, 2012, 10:02:54 PM »

Hey Keyrocks!

The code for the "Cannot modify header information"
Is not working (at list for me)
I have the same warning plus redirects to

Quote
http://127.0.0.1/snews-original//

this is the code I use
Code: [Select]
if(isset($_POST['Loginform']) && !_ADMIN) {
$user = checkUserPass($_POST['uname']);
$pass = checkUserPass($_POST['pass']);
unset($_POST['uname'],$_POST['pass']);
# Validates Username, Password and MatchCaptcha (if enabled)
if (md5($user) === s('username') && md5($pass) === s('password') && checkMathCaptcha()) {
# Validates Username and Password Only
//if (md5($user) === s('username') && md5($pass) === s('password')) {
$_SESSION[_SITE.'Logged_In'] = token();
notification(2,'','');  // (error, note, link) - Removed 'administration' from link parameter to show home page.
} else {
die( notification(2,l('err_Login'),'login'));
}
}
if(isset($_POST['submit_text']) && !_ADMIN){   // Added: isset() to eliminate PHP warning notice
die (notification(2,l('error_not_logged_in'),'home'));
}


All the others "isset" and "clobals" I think they work just fine!

The biggest notices generate in processing()
for example
as Administration go to Settings change anything in Contents and save......  ???


Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #9 on: February 12, 2012, 04:07:26 AM »

Hey Keyrocks!
The code for the "Cannot modify header information" Is not working (at list for me)
I have the same warning plus redirects to

Quote
http://127.0.0.1/snews-original//

Yes... the double slashes in the URL.
The extra slash is put there because it is at the end of this string down in the //NOTIFICATION function:
   echo '<meta http-equiv="refresh" content="5; url='._SITE.$link.'/">';
... so it ends up in the URL when we remove the link parameter administration in:
   notification(2,'',''); // parameters (error type, note, link)

... so the way the notifications work it needs to have a link parameter... I tried using home but get a "no content" message. So I will leave it as it was with administration for now.

The biggest notices generate in processing()
for example - as Administration go to Settings change anything in Contents and save......  ???

Thanks... I didn't check saving there... that lists a LOT of Notices...
UPDATED: I tested patches to function processing() but they caused all of the values in the d-base setting tables to be updated to "1" - bad - so don't use them as they are now !!!   :-[
« Last Edit: February 12, 2012, 06:14:26 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information [SOLVED]
« Reply #10 on: February 12, 2012, 06:44:27 AM »

Well, first of all sorry that I am asking about that
For sure I don't have the knowledge (as I want) about PHP

But I don't understand why
when you login
(although the parameters in function notification is (error type, note, link)
WHY when login discover an error type???
I think that is not normal!!

In center()
have          
case isset($_POST['Loginform']):
administration(); return; break;

So why exist such a warning, and why pass as $error == 2

I need one explanation, not only for me but for everyone that involve in any way to sNews

Also in function center() (at the beginning have)
Code: [Select]
  if (isset($_SESSION[_SITE.'fatal'])) {
echo $_SESSION[_SITE.'fatal'];
unset($_SESSION[_SITE.'fatal']);

The question is, how you determined that something is "FATAL" ???

I hope I don't mess up with anything, and I hope with my questions helps to be a better sNews!!!
« Last Edit: February 12, 2012, 07:11:54 AM by sibas »
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #11 on: February 12, 2012, 06:18:07 PM »

WARNING: Do not use the patches to function processing() I posted last night. Something in them caused all of the values in the d-base setting tables to be updated to "1" - bad - so this is getting more complicated than I expected... at least for me.   :-[
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #12 on: February 12, 2012, 11:25:15 PM »

After logging in - redirects to

Quote
http://127.0.0.1/snews-original//

.

Here is a better work-around for this.
   echo '<meta http-equiv="refresh" content="5; url='._SITE.$link.'/">';
... in the //NOTIFICATION function... is only used (for error type 2) in three places, all close together in the script that checks the login data.

So... we can remove the slash from that string in the //NOTIFICATION function, and just add it to the end of the link parameter text in the 2nd and third strings... and leave the first one's link parameter empty.
(search for notification(2 to find all three strings)

1st string would now be: notification(2,'');
2nd string would now be: die( notification(2,l('err_Login'),'login/'));
3rd string would now be: die (notification(2,l('error_not_logged_in'),'home/'));

The 1st string immediately directs the logged-in user to the Home page with the URL formed properly.
The 2nd and third strings continue to work as intended with the slash in place at the end of the link parameter.

This does not affect the 13 notification(0 strings or the 26 notification(1 strings.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Cannot modify header information [SOLVED]
« Reply #13 on: February 13, 2012, 06:56:29 AM »

OK the slash redirect problem is not problem :)
But still have the same warning "Cannot modify header information - headers already sent"
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Cannot modify header information [SOLVED]
« Reply #14 on: February 13, 2012, 05:12:16 PM »

OK the slash redirect problem is not problem :)
But still have the same warning "Cannot modify header information - headers already sent"

Have you now got this section relocated so that it is now above the //TITLE function like so?
Code: [Select]
<?php // DO NOT COPY THIS TAG

if(isset($_POST['Loginform']) && !_ADMIN) {
$user checkUserPass($_POST['uname']);
$pass checkUserPass($_POST['pass']);
unset($_POST['uname'],$_POST['pass']);
// Patch #18 - 1.7.1 - revised string by KikkoMax
if (checkMathCaptcha() && md5($user) === s('username') && md5($pass) === s('password')) {
//if (md5($user) === s('username') && md5($pass) === s('password') && checkMathCaptcha()) {
$_SESSION[_SITE.'Logged_In'] = token();
notification(2,'','administration');
} else { die( notification(2,l('err_Login'),'login')); }
}
if(isset(
$_POST['submit_text']) && !_ADMIN){   // Added: isset() to eliminate PHP Notice
die (notification(2,l('error_not_logged_in'),'home'));
}

//TITLE
function title() {

// DO NOT COPY THIS TAG ?>


The location is vital - it must be here so that it is parsed after  header('HTTP/1.1 404 Not Found'); is parsed - this is how I have it in my test file and the "Cannot modify header information - headers already sent" warning is gone for me.
« Last Edit: February 14, 2012, 08:10:06 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU
Pages: [1] 2