robert_gsfame Posted September 26, 2009 Share Posted September 26, 2009 See i always get this error when start using header('location What cause this and how to prevent this happen..Please solve this problem Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/ Share on other sites More sharing options...
smerny Posted September 26, 2009 Share Posted September 26, 2009 you can not use header() after you have already declared the header... or output any html... either restructure your page or use a meta refresh Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925338 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 please explain this to me, im still newbie on this...then what should i do to redirect to other page ? Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925340 Share on other sites More sharing options...
PFMaBiSmAd Posted September 26, 2009 Share Posted September 26, 2009 A header() redirect must come before any other output is sent to the browser. If you could actually send output to the browser, then redirect, it would waste your hosting bandwidth every time it did it, so, you are actually lucky that you cannot send output before a header. The specific error message that you get tells you where the output is being started at that is preventing the header() from working. You must find and either eliminate the output (some times it is due to an incorrectly saved file format) or move the output so that it occurs after the header() or move the header() so that it is before any output. Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925342 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 is it okay to do this...to redirect to other page??any problem with this print "<script>"; print "self.location='blabla.php'"; print "</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925343 Share on other sites More sharing options...
monkeytooth Posted September 26, 2009 Share Posted September 26, 2009 The short, simple crude version is.. once that <html> tag is output and read by your browser, there is no room for more headers to be sent out. They have already been read and interpreted. So if you need to have modified headers of any sort they must be setup prior to the <html> tag. However, don't output anything other then those headers prior to the <html> tag, or you will end up with other errors, be it with the PHP itself, or cross browser compatibility issues or what ever.. So I am assuming your attempting to do something with sessions, as thats what I see most commonly with the error your mentioning. If so, modify your site by going a line above <html> and put <?php session_start(); /*header stuff */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925344 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 that will be fine, for people who have javascript enabled! but everyone else will have problems quick problem example's header("Location: login.php"); //will redirect echo "Hello"; //won't display echo "Hello"; //will display header("Location: login.php"); //will fail echo "Hello"; //will display Hello <?php header("Location: login.php"); //will fail echo "Hello"; //will display ?> as you can see ANY output will cause the header to fail.. if you post your code (in code tags) I'm sure someone give you a better option Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925345 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 thanx, one more question...how do users know that their javascript is enabled or disabled??? and which browsers disable the use of javascript?? Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925347 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 thanx, one more question...how do users know that their javascript is enabled or disabled??? Many ways.. I normally use some JavaScript to detect if its on. and which browsers disable the use of javascript?? its an option in all browsers. Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925351 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 which means we cannot automatically activate javascript in users' browser once they visit our site??? Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925355 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 No you can't.. .. if you could that would be hell for the users.. IMHO headers is a better option, do you have some code you are attempting to get working with headers ? Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925361 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 ha...ha...ha...ha... yeah this is what i want to do let say in page1.php, i have a form and once submit button was clicked then it will go to page2.php. page2.php only will be used as a page to run Delete query , so i only put Delete query... n it immediately goes back to page1.php after all queries have been executed... that's why i put header('locationXXXXXXXXXXXX after i run mysql_query("DELETE FROM xxxxxxxxxxxxxxxxxxxxxxxxxx n it goes ERRROOORRRRRR Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925370 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 okay.. first off, I wouldn't post to page2.php i would post to page1.php and have an if statement to capture the request.. ie <?php if(!empty($_POST['DELETE'])) { $ID = (int) $_POST['DELETE']; //DELETE FROM table where ID= $ID } ?> //form here but could you post your page2.php Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925373 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 okay this is the code <?php session_start();?> <?php require_once('configuration.php');?> <?php $user=$_SESSION['user']; $id=$_GET['id']; $query="DELETE FROM table WHERE user='$user' AND id='$id'"; $sqlquery=mysql_query($query); print "<script>"; print "self.location='page2.php'"; print "</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925377 Share on other sites More sharing options...
robert_gsfame Posted September 26, 2009 Author Share Posted September 26, 2009 sorry wrong code for this part it should be self.location:page1.php Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925379 Share on other sites More sharing options...
PFMaBiSmAd Posted September 26, 2009 Share Posted September 26, 2009 For that short bit of code, finding and eliminating the output that is causing the header error would be simple, except that it would take seeing the actual error message as that states where the output is occurring at. xxxxx out any sensitive information in the error message, but you have got to supply all the relevant information if you want someone else to help find the cause of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925389 Share on other sites More sharing options...
MadTechie Posted September 26, 2009 Share Posted September 26, 2009 try this <?php session_start(); require_once('configuration.php'); $user=$_SESSION['user']; $id=(int)$_GET['id']; $query="DELETE FROM table WHERE user='$user' AND id='$id'"; $sqlquery=mysql_query($query); header("Location: page2.php"); have NOTHING before the <?php (including a space or return) also update table to the table name EDIT: oops missed a ?> Quote Link to comment https://forums.phpfreaks.com/topic/175608-cannot-modify-header-information-headers-already-sent-by/#findComment-925390 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.