UnknownPlayer Posted November 29, 2010 Share Posted November 29, 2010 Why do i get this error when i put code for redirection on my php code :S Warning: Cannot modify header information - headers already sent by (output started at /home/dotars/public_html/shop/includes/functions.php:3) in /home/dotars/public_html/shop/includes/functions.php on line 20 This is that code on functions.php on line 20: function confirm_logged_in() { if (!logged_in()) { redirect_to("login.php"); } } function redirect_to($location = NULL) { if ($location != NULL) { header("Location: {$location}"); exit; } } And this is the code in the other php code: <?php require_once("includes/session.php"); ?> <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php confirm_logged_in(); ?> <!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" /> <title>Untitled Document</title> </head> <body> <center> Text... </center> </body> </html> And confirm_logged_in() function is: Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2010 Share Posted November 29, 2010 output started at /home/dotars/public_html/shop/includes/functions.php:3 ^^^ Something on or up to line 3 in the functions.php file is sending output. You would need to find and eliminate that output. Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141010 Share on other sites More sharing options...
ManiacDan Posted November 29, 2010 Share Posted November 29, 2010 The header() function cannot operate properly if anything has been sent to the browser. As PFMaBiSmAd pointed out, line 3 of your functions file has output. You must remove it in order for header() to work. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141011 Share on other sites More sharing options...
DJTim666 Posted November 29, 2010 Share Posted November 29, 2010 Add this to your file before anything is written or included. <?php ob_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141012 Share on other sites More sharing options...
UnknownPlayer Posted November 29, 2010 Author Share Posted November 29, 2010 Add this to your file before anything is written or included. <?php ob_start(); ?> What that code means? On line 3 in functions.php file was now row, empty, i deleted it and it works, i dont know what was the problem :S Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141013 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 If it works now, there is no need for ob_start() at all. Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141016 Share on other sites More sharing options...
UnknownPlayer Posted November 29, 2010 Author Share Posted November 29, 2010 But what this function do ? And will this function do something wrong if i put it anyway? Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141022 Share on other sites More sharing options...
ManiacDan Posted November 29, 2010 Share Posted November 29, 2010 To find out what a function does, read the manual entry for that function. Output buffering is a complicated topic, you don't need it unless you know you need it. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141029 Share on other sites More sharing options...
gizmola Posted November 29, 2010 Share Posted November 29, 2010 A "pro" php tip is to always omit the ending php tag from any script that only includes PHP code. A line or 2 of whitespace can inadvertantly cause output to be triggered in scripts where you never intended that to happen -- like your functions.php include script. Whenever a script is included, you have to realize that it is injected as is, into the including script. Since PHP allows for the intermixing of PHP and HTML. Leaving off the end tag will eliminate that problem. Quote Link to comment https://forums.phpfreaks.com/topic/220157-cannot-modify-header-information-headers-already-sent-by/#findComment-1141035 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.