Jump to content

Warning: Cannot modify header information


clonetrooper9494

Recommended Posts

I have gotten an error:Warning: Cannot modify header information - headers already sent by (output started at http://clone-drone.net/php_scripts/navigat...a=&link=:2) in /home/clonedro/public_html/login/login.php on line 17this is line 17: header('Location: http://clone-drone.net/');//just an example pageit is in the middle of an else... how can I get around this? basically, I am trying to redirect the user in the middle of an else. I was also trying to set a cookie... how can I get around this problem? is there just something wrong with my host?the page is http://www.clone-drone.net/login/login.php

Link to comment
Share on other sites

I have gotten an error:Warning: Cannot modify header information - headers already sent by (output started at http://clone-drone.net/php_scripts/navigat...a=&link=:2) in /home/clonedro/public_html/login/login.php on line 17this is line 17: header('Location http://clone-drone.net/');//just an example pageit is in the middle of an else... how can I get around this? basically, I am trying to redirect the user in the middle of an else. I was also trying to set a cookie... how can I get around this problem? is there just something wrong with my host?the page is http ://www.clone-drone.net/login/login.php
I think you're mising a colon ( : ) there:header('Location: htt p://clone-drone.net/');
Link to comment
Share on other sites

here is the full code:

<?phpinclude("http://clone-drone.net/php_scripts/navigate.php?extra=&link=");$u = (get_magic_quotes_gpc()) ? $_POST['user'] : mysql_real_escape_string($_POST['user']);$p = (get_magic_quotes_gpc()) ? $_POST['pass'] : mysql_real_escape_string($_POST['pass']);$con = mysql_connect('localhost',**********,'**********');mysql_select_db("clonedro_login", $con);$result = mysql_query("SELECT * FROM test_login WHERE username = '$u' AND password = '$p'");if(mysql_num_rows($result)==1){echo "yay " . $u;// . $_COOKIE['username'];//setcookie("username",$u,time()+3600,NULL,'.clone-drone.net');//setcookie("password",$p,time()+3600,NULL,'.clone-drone.net');}else{echo "oh no!";header('Loctaion: http://clone-drone.net');}?>

Link to comment
Share on other sites

Ok justsomeguy, I have read your posts the topic: PHP Tip and Tutorials and I still don't see why this is happening:this is the pages full source:

<?phpinclude("http://clone-drone.net/php_scripts/navigate.php?extra=&link=");$u = (get_magic_quotes_gpc()) ? $_POST['user'] : mysql_real_escape_string($_POST['user']);$p = (get_magic_quotes_gpc()) ? $_POST['pass'] : mysql_real_escape_string($_POST['pass']);$con = mysql_connect('localhost',***********,'************');mysql_select_db("clonedro_login", $con);$result = mysql_query("SELECT * FROM test_login WHERE username = '$u' AND password = '$p'");if(mysql_num_rows($result)==1){header('Loctaion: http://clone-drone.net/login_php.php');}else{header('Loctaion: http://clone-drone.net/oops.html');}?>

Link to comment
Share on other sites

I don't understand why this is so difficult for people to figure out. The error message says exactly and specifically what the problem is and where it is. Look at the freakin thing:Warning: Cannot modify header information - headers already sent by (output started at http://clone-drone.net/php_scripts/navigat...a=&link=:2) in /home/clonedro/public_html/login/login.php on line 17There you go. There is a specific file and line number where it is saying there is output being sent to the browser. There is output in that file, on line 2. Hopefully it helps that I bolded, underlined, and italicized that part. That include statement at the very top of your file which includes the file causing the error may be something you want to take a look at.

Link to comment
Share on other sites

Ok... I play around with it... and I have found that PHP doesn't like me :) ! here is the new message:Warning: Cannot modify header information - headers already sent by (output started at /home/clonedro/public_html/login/index.phtml:5) in /home/clonedro/public_html/login/index.phtml on line 35Warning: Cannot modify header information - headers already sent by (output started at /home/clonedro/public_html/login/index.phtml:5) in /home/clonedro/public_html/login/index.phtml on line 36I am assuming that :5 means line 5...here is my code:

<html><head><title>Clone-Drone.net - Login</title><script>//line 5...function check_username(){u = document.getElementById('username').value;if(u == 'Username'){document.getElementById('username').value = '';}}function check_password(){document.getElementById('password_div').innerHTML = '<input type="password" name="pass">';}</script></head><body bgcolor="#FFFFFF"><?phpif(sizeof($_POST)>2){$u = (get_magic_quotes_gpc()) ? $_POST['user'] : mysql_real_escape_string($_POST['user']);$p = (get_magic_quotes_gpc()) ? $_POST['pass'] : mysql_real_escape_string($_POST['pass']);$con = mysql_connect('localhost',************,***********);mysql_select_db("clonedro_login", $con);$result = mysql_query("SELECT * FROM test_login WHERE username = '$u' AND password = '$p'");if(mysql_num_rows($result)==1){echo "yay";setcookie("username",$u,time()+3600,NULL,'.clone-drone.net');//line 35setcookie("password",$p,time()+3600,NULL,'.clone-drone.net');//line 36}else{echo "oh no!";}}include("http://clone-drone.net/php_scripts/navigate.php?extra=&link=");?><form action="/login/index.phtml" method="post"><input type="text" name="user" value="Username" onclick="check_username()" id="username"><br><div id="password_div"><input type="text" name="pass" value="Password" onclick="check_password()"></div><br><input type="submit" name="submit" value="Login"></form></body></html>

instead of having the include first, I have the cookies first... but a new error pops up. it tells me that the headers are being sent out at line 5... which is a <script> tag! PHP must not like me :) Could anyobdy explain to me why this is happning?

Link to comment
Share on other sites

Did you actually read the PHP tips topic about this? It's not that PHP doesn't like you, PHP is working exactly as it's supposed to, it's that you don't understand PHP.

To solve this warning, identify the point at which you send output, and move the code that is sending the header before any output. Ideally, you should have all of your PHP code on the top of your file only, and all HTML code after that. All PHP processing should be finished by the time you start sending output to the browser.
It shouldn't be identifying the <script> tag as the beginning of output, it should be identifying the <html> as the start of the output. From reading the topic I'm quoting you should be aware that trying to send a cookie after any output is going to cause this error. In your code, you send a ton of HTML code, then echo something (all of which is output!), THEN try to set a cookie. I've highlighted everything in your code before you set a cookie that is output that gets sent to the browser.<html><head><title>Clone-Drone.net - Login</title><script>//line 5...function check_username(){u = document.getElementById('username').value;if(u == 'Username'){document.getElementById('username').value = '';}}function check_password(){document.getElementById('password_div').innerHTML = '<input type="password" name="pass">';}</script></head><body bgcolor="#FFFFFF"><?phpif(sizeof($_POST)>2){$u = (get_magic_quotes_gpc()) ? $_POST['user'] : mysql_real_escape_string($_POST['user']);$p = (get_magic_quotes_gpc()) ? $_POST['pass'] : mysql_real_escape_string($_POST['pass']);$con = mysql_connect('localhost',************,***********);mysql_select_db("clonedro_login", $con);$result = mysql_query("SELECT * FROM test_login WHERE username = '$u' AND password = '$p'");if(mysql_num_rows($result)==1){echo "yay";setcookie("username",$u,time()+3600,NULL,'.clone-drone.net');//line 35Any of those bold lines coming before the line to setcookie is going to cause this error. Let me quote this again..
To solve this warning, identify the point at which you send output, and move the code that is sending the header before any output. Ideally, you should have all of your PHP code on the top of your file only, and all HTML code after that. All PHP processing should be finished by the time you start sending output to the browser.
Instead of finishing PHP processing before you start sending output, you start sending output and then start the PHP processing. Why is that block of PHP code inside the body tag and not at the very top of the page?
Link to comment
Share on other sites

Ok... I play around with it... and I have found that PHP doesn't like me :) ! here is the new message:Warning: Cannot modify header information - headers already sent by (output started at /home/clonedro/public_html/login/index.phtml:5) in /home/clonedro/public_html/login/index.phtml on line 35Warning: Cannot modify header information - headers already sent by (output started at /home/clonedro/public_html/login/index.phtml:5) in /home/clonedro/public_html/login/index.phtml on line 36I am assuming that :5 means line 5...here is my code:
<html><head><title>Clone-Drone.net - Login</title><script>//line 5...function check_username(){u = document.getElementById('username').value;if(u == 'Username'){document.getElementById('username').value = '';}}function check_password(){document.getElementById('password_div').innerHTML = '<input type="password" name="pass">';}</script></head><body bgcolor="#FFFFFF"><?phpif(sizeof($_POST)>2){$u = (get_magic_quotes_gpc()) ? $_POST['user'] : mysql_real_escape_string($_POST['user']);$p = (get_magic_quotes_gpc()) ? $_POST['pass'] : mysql_real_escape_string($_POST['pass']);$con = mysql_connect('localhost',************,***********);mysql_select_db("clonedro_login", $con);$result = mysql_query("SELECT * FROM test_login WHERE username = '$u' AND password = '$p'");if(mysql_num_rows($result)==1){echo "yay";setcookie("username",$u,time()+3600,NULL,'.clone-drone.net');//line 35setcookie("password",$p,time()+3600,NULL,'.clone-drone.net');//line 36}else{echo "oh no!";}}include("http://clone-drone.net/php_scripts/navigate.php?extra=&link=");?><form action="/login/index.phtml" method="post"><input type="text" name="user" value="Username" onclick="check_username()" id="username"><br><div id="password_div"><input type="text" name="pass" value="Password" onclick="check_password()"></div><br><input type="submit" name="submit" value="Login"></form></body></html>

instead of having the include first, I have the cookies first... but a new error pops up. it tells me that the headers are being sent out at line 5... which is a <script> tag! PHP must not like me :) Could anyobdy explain to me why this is happning?

You have to send the headers before writing anything at all onto the page, that includes HTML.
Link to comment
Share on other sites

Even things outside of PHP tags is considered output, this won't work:

<html><?php header("location:index.php"); ?>

Link to comment
Share on other sites

Even things outside of PHP tags is considered output, this won't work:
<html>	   <?php header("location:index.php"); ?>

Heck, this wouldn't work
   <?php  header('location:index.php');

(theres just 1 space before the opening php tag

Link to comment
Share on other sites

   <?php  header('location:index.php');

(theres just 1 space before the opening php tag

I count three :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...