matleeds Posted September 16, 2011 Share Posted September 16, 2011 hi there, seems like I'm getting a fairly common beginners php error but as I'm using a number of differant files and as I'm not entirley clear on a couple of things I thought I'd tap into the immense php talent on this site here's the error - Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Epointment\index.php:4) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Epointment\ot\views\header.php on line 38 I have an index page that allows a user to log on, the processing go's away and checks the usual stuff then reloads the login page with the user detail or just reloads the login page with the user/password fields blank. This seemed to be working ok until I put my files onto another web server that had output_buffering turned off. here's part of the login page <html> <head><title></title></head> <body> <?php ini_set("display_errors", "1"); error_reporting(-1); $dir = dirname(__FILE__); require_once "$dir/ot/ot.php"; ot::include_view('header', array('account' => null)) ?> // various stuff <p>Please login below</p> <?php ot::include_view('login_form')?> the ot.php file doesn't have any echo's or print_r's etc if that helps?? here's the header.php file.. <?php if (ot::get('do_logout', 0) == 1) { ot::destroy_session(); header("Location: http://{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']}"); exit; } elseif (self::post('do_reset_password')) { ot::do_reset_password($err); echo "$err"; } $adb = ot::db('account'); $account = null; $error = ""; $id = ot::account_id(); if ($id) { $account = ot::account_from('id', $id); $res = ot::do_change_password($account, $err); if ($res) { echo "<p>Password Changed Successfully</p>"; $account = $res; } else if ($err) { echo "<p>Error: $err</p>"; } $res = ot::do_change_email($account, $err); if ($res) { echo "<p>Email Changed Successfully</p>"; $account = $res; } else if ($err) { echo "<p>Error: $res</p>"; } } else { $account = ot::do_login(); if ($account) { header("Location: http://{$_SERVER['SERVER_NAME']}{$_SERVER['PHP_SELF']}"); exit; } } ?> here's the ot::do_login function public static function do_login(&$err="") { $adb = ot::db('account'); $e = self::post('email'); $p = self::post('pwd', '', false); if (self::post('do_login') && $e && $p) { $ao = self::account_from('email', $e); if ($ao) { if (self::validate_login($e, $p, $ao)) { //echo "\n"."hit2"; $_SESSION['id'] = $ao->id; return $ao; } } $err = "Invalid email or password"; return false; } } and here's the login_form.php, which is called from ot::do_login from header.php ... <form action='<?php echo $_SERVER['REQUEST_URI']?>' method='post' > <p>Email:<br/><input type='text' name='email' /></p> <p>Password:<br/><input type='password' name='pwd' /></p> <p><input type='submit' name='do_login' value='Login' /> <input type='submit' name='do_reset_password' value='Reset Password' /></p> </form> Any pointers or useful things I've not posted? is this bit of code from above ok? as in I'm doing some php stuff before the header() function gets used? <html> <head><title></title></head> <body> <?php ini_set("display_errors", "1"); error_reporting(-1); $dir = dirname(__FILE__); require_once "$dir/ot/ot.php"; ot::include_view('header', array('account' => null)) Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/ Share on other sites More sharing options...
AyKay47 Posted September 16, 2011 Share Posted September 16, 2011 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Epointment\index.php:4 this is where the ouput is started.. wheres that line of code here Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269893 Share on other sites More sharing options...
matleeds Posted September 16, 2011 Author Share Posted September 16, 2011 it's the fourth line down in the first code snippet <?php Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269895 Share on other sites More sharing options...
matleeds Posted September 16, 2011 Author Share Posted September 16, 2011 line 38 is the 5th'ish line up from the second code snippet...where the header() function is Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269897 Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 This is already the output(*): <html> <head><title></title></head> <body> After it you should not use header function. But you did as I see this code ot::include_view('header', array('account' => null)) go after (*) and make some redirect Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269902 Share on other sites More sharing options...
jcbones Posted September 16, 2011 Share Posted September 16, 2011 Move all of your processing to the very TOP of your first code snippet. That would mean the included file. Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269907 Share on other sites More sharing options...
matleeds Posted September 16, 2011 Author Share Posted September 16, 2011 thanks guys, that's sorted it. I've moved the <html> tags down in the script so I can understand this, even having the standard <html> <head><title> etc tags means that the php parser automaticaly sends a header back to the client browser Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269915 Share on other sites More sharing options...
jcbones Posted September 16, 2011 Share Posted September 16, 2011 Yes, anything sent to the screen, either plain HTML, or an echo, print, or a function that prints to the page (including display_errors) will send the headers to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/247271-header-error-i-have-read-the-post-at-the-top-already/#findComment-1269940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.