logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



Cannt send header problem

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Thu 16 Jul '09 14:17    Post subject: Cannt send header problem Reply with quote

Hi all , i have problem regarding sending the headers in the script. I am getting problem while i send header , it says that the header is already send at so and so line no.
Actually the same code is running nicely on my laptop but if i run on different PC, it gives that error. so i am confused wat to do. I m posting the code of one page but , but in the forum if i post a reply or if i add new topic same the headers are send so i am giving just one code and , due to that problem it is not possible for me do the sessions as well so plz help me wat to do.
Code:

$link=mysqli_connect($host,$username,$password,$database);
   if(!$link)
   {
      die('Could not connect: ' . mysqli_error());
   }
   else
   {
      $post_id = $_REQUEST['post_id'];
   
      
         if (!isset($post_id))
         {
            header("Location : topiclist.php");
            exit;
         }
   //still have to verify topic and post
      $verify_sql="select ft.topic_id,ft.topic_title from forum_posts
                  AS fp LEFT JOIN forum_topics AS ft on
                  fp.topic_id=ft.topic_id where fp.post_id='".$post_id."'";
                  
      $verify_res =mysqli_query($link,$verify_sql) or die(mysqli_error($link));
      
      if (mysqli_num_rows($verify_res)<1)
            {
               // this post or topic does nt exit
               header("location : topiclist.php");
               exit;
            }
      else
      {
            // get the topic id and title
            while($topic_info=mysqli_fetch_array($verify_res))
         {
            $topic_id = $topic_info['topic_id'];
            $topic_title=stripslashes($topic_info['topic_title']);
         }
         //echo "
      ?>      
       
           Post Your Reply in  <strong><?php echo $topic_title ?></strong><br />
           <br />
           </div>
          <form method="post" action="postreply.php" style="text-align:center" >
  <table width="609" border="0" align="center">
    <!--DWLayoutTable-->
                <tr>
                    <td width="197" height="24" valign="top"><div align="right"><strong>Subject<br />
                  </strong></div></td>
                    <td width="320" valign="top">
                      <div align="left">
                        <input type="text" name="post_title" size="60" maxlength="150"  />
                        </div></td>
                    <td width="67">&nbsp;</td>
                  <td width="6">&nbsp;</td>
                </tr>
                  <tr>
                    <td height="245" align="left" valign="top"><div align="right"><strong>Message</strong></div></td>
                    <td colspan="2" valign="top"><textarea name="post_text" rows="15" cols="60" wrap="virtual"> </textarea></td>
                    <td>&nbsp;</td>
                  </tr>
              </table>
                <p>
                  <input type="hidden" name="topic_id" value="<?php echo $topic_id ?>" />
                  <br />
                </p>
                <p>
                 <input type="submit" name="Submit" value="Add Post" />
                </p>
                     <?php
        }   
         
   //free result
   mysqli_free_result($verify_res);
   
   //close connection to mysql
   
   mysqli_close($link);
      
   }
                                                       
?>
       </p>
        </form>      </td>
      </tr>
    </table>    </td>
</tr>
  <tr>
    <td height="276" valign="top" bgcolor="#DBE8FB" ><!--DWLayoutEmptyCell-->&nbsp;</td>
  </tr>
</table>
</body>
</html>



So in the above code there is header send , so its not working properly i have to redirect it using "Continue". Please help
Thanks in advance
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Thu 16 Jul '09 15:36    Post subject: Reply with quote

There is a "bit" dirty trick to prevent that.

Put at the very to in your php file after <?php

Code:

ob_start();


Than PHP caches all output into a buffer and sends the content after running the full script. So apache won't send a header before your header.
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Fri 17 Jul '09 10:51    Post subject: Cannt send header problem Reply with quote

Hi james thanks for your reply , i tried the thing that u told me i put, ob_start() after the tag <?php but still its not working situation is same. Dont know wat to do
Code:


 <?php
   ob_start();
   $username = "root";
   $password = "";
   $host = "";
   $database = "mydb";
   
   $link=mysqli_connect($host,$username,$password,$database);
   if(!$link)
   {
      die('Could not connect: ' . mysqli_error());
   }
   else
   {
      $post_id = $_REQUEST['post_id'];
   


Like above i added ob_start(), but still the condition is same so plz guide me wat can i do
thanks in advance
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Sat 18 Jul '09 10:15    Post subject: Reply with quote

Please post the generated source from your browser with that error message.
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Sat 18 Jul '09 14:12    Post subject: header not send Reply with quote

hi james thanks for the reply, as per ur suggestion i inserted the code ob_start(), still the error is same, as after login i am trying to get the username and pass in session due to this problem i am not able to store the session variables, for the sessions it gives following error

Code:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\designmagicworks\forumindex.php:8) in C:\xampp\htdocs\designmagicworks\forumindex.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\designmagicworks\forumindex.php:8) in C:\xampp\htdocs\designmagicworks\forumindex.php on line 10





and for the error of header error it gives following error
Code:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\designmagicworks\do_addtopic.php:12) in C:\xampp\htdocs\designmagicworks\do_addtopic.php on line 69.

I am confused as i m nt getting any solution for this.
Does is this problem due to XAMP, as same code is running perfectly on my laptop so i am confused.
Thanks in advance
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Sat 18 Jul '09 17:58    Post subject: Reply with quote

Where in the code do you start the session? I can't see it the in examples you posted.
You have to put the session_start() directly under ob_start();

If you use includes make sure that you only start the session once.
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Sun 19 Jul '09 15:00    Post subject: header problem Reply with quote

hi james thanks for the reply ,
actually the thing is just the one of the code i posted , actually the login code is in the other file , but its just the same and simple code as we have normally i have not used the include file , i have directly taking username and pass, so if u want i wil post that code too, i m really dont know wats the problem, i wil try to post the code with session_start after ob_start. Is there any alternative for the problem ,basically wats the mistake that i have made can u tel me plz
thanks again for ur help
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 20 Jul '09 8:39    Post subject: Header not send properly Reply with quote

Hi james as per ur request i m sending the login script.

Code:


ob_start();
session_start();
$username = "root";
$password = "";
$host = "localhost";
$database = "abc";
$link=mysqli_connect($host,$username,$password,$database);
   
   if(!$link)
   
   {
      die('Could not connect: ' . mysqli_error());
   }
   
   else
   
   {   
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
//echo $mypassword;
   
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($link,$myusername);
$mypassword = mysqli_real_escape_string($link,$mypassword);


$sql="SELECT * FROM login WHERE username='$myusername' and password='$mypassword'";

echo $sql;

$result=mysqli_query($link,$sql);

$count= mysqli_num_rows($result);
// Mysql_num_row is counting table row
//$count=$r[0];
// If result matched $myusername and $mypassword, table row must be 1 row
if($count>=1)
{

// Register $myusername, $mypassword and redirect to file "login_success.php"
//session_register("myusername");
//session_register("mypassword");
while($row=mysqli_fetch_array($result))
{

$temp=$row['password'];
echo $temp;
}

$_SESSION["uname"]=$myusername;
$_SESSION["pass"]=$temp;
//echo "this is session value ".$_SESSION["uname"];
//echo"this is loginid from login table". $_SESSION["pass"];
if ($myusername=='abc' or $myusername=='def')
{
   echo "abc logged in ";
   header("location:forumindex.php");
}

else
{
//echo "login Successful";
//echo "<a href =\"forumindex.php\"> Continue </a>";
header("location:forumindex.php");
//echo "this is session memeber id " .$_SESSION["memberno"];
}
}
else
{
echo "<br><br>";
echo "<h4 >Either Userame or Password is incorrect <br> Please re-enter the password or username</h4>";
echo "<a href='forumlogin.php'>BACK </a>";
}      
}
?>
</body>
</html>




This is the code for login form, and it gives above error the one in my previous post.


Actually i am using the same code for another web site which require login, but its working all well. so wat is the problem, actually the thing is session is working i think header is not getting send. So i request you to please let me know where is the problem.

And Apart from the this , i want to post here some code , related php, for uploading images and pagination related script which u helped for that, in which category i should post that. please let me know
Thanks in advance
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Mon 20 Jul '09 10:13    Post subject: Reply with quote

The code above works fine for me.

I see only two error!


You can't send a header after echo something or giving outout.

first
Code:

echo $sql;


second
Code:

if ($myusername=='abc' or $myusername=='def')
{
   echo "abc logged in ";
   header("location:forumindex.php");
}



Also you should put a die(); after the header to make sure that there won't be any output after it. e.g. </body></html>
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 20 Jul '09 10:44    Post subject: header not send Reply with quote

Thanks for reply , i wil check that rt now and send u back, i wil remove that "echo $sql" statement and for the 2nd problem ,its working fine for me in my other web site, so there is no problem for me regarding that code. I wil use that die(); after every header statement.
I asked you regarding posting the code , i request you to plz let me know in which category should i post in the forum.
Thanks in advance
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Mon 20 Jul '09 11:29    Post subject: Header not send properly Reply with quote

Hey james
the problem is still the same , no solution Sad , i checking it , i am nt getting the solution , thanks for your help i wil try to find some other way and let u know thanks for the help. If u found any other alternative then plz let me know.
Thanks in advance
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Mon 20 Jul '09 17:02    Post subject: Reply with quote

Well I've tested the script with a dummy database and here with PHP 5.2.10 it works fine.

I've posted my php.ini so you can checkout what is different to yours.

http://pastebin.com/me035367
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Tue 21 Jul '09 9:23    Post subject: header not send Reply with quote

hi james i really thankful for taking efforts for me , i checked ur php.ini and mine and i found out following two differences.

in ur php.ini the lines are like following

memory_limit = 256M
output_buffering = on

my php.ini is as below.

memory_limit = 128M
output_buffering = Off

does this output buffering is making any problem for me, but if its like that then my other web sites is using same so i am nt getting problem with header for other web sites those i am making. And most important thing is i am using Xamp not individual installation. So i dont know wats the problem. But i really appreciate ur efforts for me.
Thanks in advance.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Tue 21 Jul '09 11:39    Post subject: Reply with quote

You can stay with your memory limit if your apps run with that. Turning on the output buffer often solve those problems and it tweaks a bit the performance.

What do you loose if you try?
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Tue 18 Aug '09 9:26    Post subject: Header Problem solved Reply with quote

Thanks James for the answer. Due to some reason i was not able to login to the forum, so i didnt got chance to reply ur post. Thanks for your help , the problem was solved that day itself. Sorry for this late reply to ur post.
Thanks
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Tue 18 Aug '09 9:58    Post subject: Reply with quote

So what is your solution?
Back to top
peacemaker



Joined: 23 May 2008
Posts: 80

PostPosted: Tue 18 Aug '09 15:01    Post subject: header not send Reply with quote

my problem was that, header was not going , u told me to change to memory limit or make autobuffer on if its off , i did that and it worked , my memory limit was 128k, i made to 256 and it work fine with me and now no problem with any header.
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1