tommyda Posted January 21, 2009 Share Posted January 21, 2009 I have read the read me before posting thread and I still cant get my head around this. Im sorry if this is an annoying question. Please help me!! Heres the error: Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/phptesting/mobile-social-networking/config.php:2) in /home/website/public_html/phptesting/mobile-social-networking/web/login.php on line 10 Cant set cookie! Heres the code: Login.php <? include("header.php"); $u = $_POST['u']; $p = $_POST['p']; $check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'")); if($check==1) { if(setcookie("BoardW",$u,time()+3600)) { Header("Location: index.php"); } else { print "Cant set cookie!"; } } else { print "User/Pass mismatch!"; } include("footer.php"); ?> Config.php <? include './paths.php'; ob_start(); mysql_pconnect(localhost,w***m,password); mysql_select_db(w***m); $password = 'password'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/ Share on other sites More sharing options...
btherl Posted January 21, 2009 Share Posted January 21, 2009 Does header.php produce any output? This includes blank lines outside the php tags. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741716 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 Something in your header.php file is outputting data to the browser. This is causing your problem. Side note: you shouldn't use short tags (<?), you should always use long tags (<?php). Short tags don't work on some servers, so if you want to move your code it won't work. And the server you are on now may suddenly change that option some day. It's sloppy programming. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741718 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 also check ./paths.php for output.. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741721 Share on other sites More sharing options...
tommyda Posted January 21, 2009 Author Share Posted January 21, 2009 Ok thanks for the tip I will use <?php from now on. There is no output in paths.php just variables the header however contains a lot of output. <? include("../config.php"); ?> <html> <head> <title><? include'../title.php';?></title> <style> * { font-family: arial; font-size: 10pt; line-height: 18px; color: #FFF; } body { background-color: #000; } a { text-decoration: underline; font-weight: bold; color: #CCFFFF; } a:hover { text-decoration: none; font-weight: bold; color: #fff; } input,select,textarea { font-family: tahoma; font-size: 10pt; color: #CCFFFF; border: 2px solid #888; background-color: #000; padding: 2px; } </style> </head> <body> <center> <table border=0 cellpadding=3 cellspacing=2 bgcolor=#FFFFFF width=600> <tr> <td colspan=2 bgcolor="#0d0d0d"><a href="index.php"><img src="../<? include'../logo.php';?>" alt="logo" /></a></td> </tr> <Tr> <td width="150" valign="top" bgcolor="#444444"><? include("nav.php"); ?></td> <td width="450" valign="top" bgcolor="#222222"> Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741722 Share on other sites More sharing options...
trq Posted January 21, 2009 Share Posted January 21, 2009 Did you really read the headers already sent sticky? You cannot send any out put to the browser prior to calling the header function. html is output. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741725 Share on other sites More sharing options...
tommyda Posted January 21, 2009 Author Share Posted January 21, 2009 Did you really read the headers already sent sticky? You cannot send any out put to the browser prior to calling the header function. html is output. I understand that completely I am asking for help on working around this because I have no idea how i'm going to do it. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741728 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 try this <?php include_once("../config.php"); $u = $_POST['u']; $p = $_POST['p']; $check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'")); if($check==1) { if(setcookie("BoardW",$u,time()+3600)) { Header("Location: index.php"); } else { $msg ="Cant set cookie!"; } } else { $msg ="User/Pass mismatch!"; } include("header.php"); echo $msg; include("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741732 Share on other sites More sharing options...
btherl Posted January 21, 2009 Share Posted January 21, 2009 It looks like login.php includes header.php which immediately includes config.php, which calls ob_start() after including paths.php. That leaves 2 options 1. Either login.php or config.php or header.php has a blank line at the top, or some kind of hidden character before the php tag 2. paths.php does produce output Try moving the ob_start() above the include of paths.php. If that doesn't work, move it to the top of login.php. If THAT doesn't work, then something is at the top of login.php. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741733 Share on other sites More sharing options...
tommyda Posted January 21, 2009 Author Share Posted January 21, 2009 I think we have established that header.php is causing the problem. I tried madtechies code and it displays the error in a different place Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741743 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 <?php ob_start(); $cookieCheck = setcookie("BoardTest","cookie",time()+3600); include("header.php"); $u = $_POST['u']; $p = $_POST['p']; $check = mysql_num_rows(mysql_query("SELECT * FROM `members` WHERE `username`='$u' AND `password`='$p'")); if($check==1) { if($cookieCheck) { ob_end_clean(); setcookie("BoardW",$u,time()+3600); Header("Location: index.php"); } else { print "Cant set cookie!"; } } else { print "User/Pass mismatch!"; } include("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741747 Share on other sites More sharing options...
tommyda Posted January 21, 2009 Author Share Posted January 21, 2009 Thank you mad techie it worked !! Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741756 Share on other sites More sharing options...
btherl Posted January 21, 2009 Share Posted January 21, 2009 I think we have established that header.php is causing the problem. But not why it causes the problem. If you establish why (by moving the ob_start() around), you can fix it. In theory, ob_start() should fix the problem if done early enough. Since it's being called but isn't fixing the problem, it must not be run early enough. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741757 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 ob_start() is just a band-aid for code that hasn't been done well. It's preferable to re-write your code so that it doesn't require buffering. Quote Link to comment https://forums.phpfreaks.com/topic/141690-headers-already-sent-please-help-with-workaround/#findComment-741759 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.