Welcome!

And don't forget to edit your signature & profile.

 

Icon

Statistics

  • Total posts 23318
  • Total topics 4048
  • Total members 5574
  • Our newest member
    ALF5583

TOP POSTERS

Warning: Cannot modify header information...

All problems and developments related to PHP, Ruby on Rails & Co. are discussed and resolved here.
   
Hello, I made a URL shortener a while back and was working ok. Today I decided I would validate the input for the custom URL field so done that with a regular expression. I uploaded and it seemed to work ok but when I went to the shortened URL I got the following message:
Warning: Cannot modify header information - headers already sent by (output started at /nfs/c05/h04/mnt/79255/domains/wixyy.com/html/includes/functions.php:1) in /nfs/c05/h04/mnt/79255/domains/wixyy.com/html/includes/functions.php on line 45


I've checked everything and can't find anything wrong. I took out the additions I made to the code and uploaded but was still faced with the same problem.
In my index.php file at the beginning I have:
Code: Select all
<?php
require 'includes/config.php';
require 'includes/functions.php';
if(isset($_GET['goto'])){
   $short=$_GET['goto'];
   gotoURL($short);
}
?>


And the gotoURL function looks like:
Code: Select all
//Goes to shortened URL
function gotoURL($short){
   $sql="SELECT link FROM shorten WHERE short='$short'";
   $result=mysql_query($sql);
   if(mysql_num_rows($result)==0){
      header('Location:?noexist');
   }else{
      while($row=mysql_fetch_array($result)){
         $url=$row['link'];
         header('Location:$url');
      }
   }
}


Has anyone any idea at all why this is happening? Was working perfectly before.

Thanks
J.Barrett
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
Usually this is due to white space or a blank line at the beginning or the end of the file... That's the first thing I would check for.
Benjamin Falk | student : designer : developer
http://www.falkencreative.com | http://www.twitter.com/falkencreative
User avatar
falkencreative
Smashing <li>
 
Posts: 261
Joined: Sun Jan 18, 2009 7:25 pm
Location: Chico, CA
   

   
Yep, checked for all that, can't see anything :(
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
What's in the Config and Functions files?
Hey look! I made a blog! http://www.msinkinson.co.uk
User avatar
TT_Mark
Smashing <li>
 
Posts: 401
Joined: Tue Feb 10, 2009 6:27 am
Location: Sheffield, UK
   

   
config.php
Code: Select all
<?php
mysql_connect('server','username','password');
mysql_select_db('dn_name');
$rootpath='http://wixyy.com';
?>


And functions contains a whole load of functions, the gotoURL one is:
Code: Select all
//Goes to shortened URL
function gotoURL($short){
   $sql="SELECT link FROM shorten WHERE short='$short'";
   $result=mysql_query($sql);
   if(mysql_num_rows($result)==0){
      header('Location:?noexist');
   }else{
      while($row=mysql_fetch_array($result)){
         $url=$row['link'];
         header('Location:$url');
      }
   }
}
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
Well, something is being printed before the headers are being sent. Make sure that there's nothing getting spit out before the headers, including error messages. You could always do an exit just after the header() command and see what's been printed.

Also, your SQL is quite insecure. Either escape it or use prepared statements (preferable).
Andy
Smashing <h5>
 
Posts: 1023
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   
There's nothing getting printed, The only thing that's being called is that gotoURL function.
Code: Select all
<?php
require 'includes/config.php';
require 'includes/functions.php';
if(isset($_GET['goto'])){
   $short=$_GET['goto'];
   gotoURL($short);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="<?php echo $rooturl;?>/css/reset.css" rel="stylesheet" type="text/css" />
<link href="<?php echo $rooturl;?>/css/style.css" rel="stylesheet" type="text/css" />
<script src="<?php echo $rooturl;?>/scripts/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="<?php echo $rooturl;?>/scripts/javascript.js" type="text/javascript" charset="utf-8"></script>
<title>Wixyy - URL Shortening for cool people =]</title>
</head>
<body>
   <div id="container">
      <div id="main">
         <img src="<?php echo $rooturl;?>/images/logo.png" alt="Wixyy, URL Shortening for cool people." class="logo"/>
         <form action="" method="post" id="shorten_url">
            <fieldset>
               <input type="text" name="url" class="url" value="Enter the URL to be shortened" onFocus="clearText(this)" autocomplete="off"/>
               <input type="submit" value="Go!" class="go" name="submit"/>
               <small class="use_custom"><a href="#">Use custom alias</a></small>
               <div class="custom_alias">
                  <input type="text" name="custom" id="custom" class="url" autocomplete="off"/>
               </div><!--close custom_alias-->
            </fieldset>
         </form>
         <div id="result">
            <?php
            if(isset($_GET['noexist'])){
               echo "The shortened URL you are trying to access has been removed or does not exist in our database.";
            }elseif(isset($_POST['submit'])){
               $url=$_POST['url'];
               $custom=$_POST['custom'];
               echo shortenURL($url,$custom);
            }
            if(isset($_GET['shorten'])){
               echo shortenURL($_GET['shorten'],'');
            }
            if(isset($_GET['safe'])){
               $short=$_GET['safe'];
               $sql="SELECT * FROM shorten WHERE short='$short'";
               $result=mysql_query($sql);
               while($row=mysql_fetch_array($result)){
                  $url=$row['link'];
               }
               echo "The URL <a href='http://wixyy.com/$short'>http://wixyy.com/$short</a> redirects to <a href='$url'>$url</a>";
            }
            ?>
         </div><!--close result-->
      </div><!--close main-->
      <div class="push"></div><!--close push-->
   </div><!--close container-->
   <div id="footer">
      Copyright &copy; <?php echo date("Y");?> <a href="http://johnathan-barrett.me.uk">Johnathan Barrett</a>
      <!-- <span class="dev"><a href="http://developer.wixyy.com">Developers</a></span> -->
   </div><!--close footer-->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7935256-3");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>


That's the rest of index.php
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
Well, you ARE including two files.

Its functions together with any whitespace that might exist will be prepended to your document. It wouldn't show that error message if there was nothing getting printed before the header.

The error message even states where the problem lies:
output started at /nfs/c05/h04/mnt/79255/domains/wixyy.com/html/includes/functions.php:1
Andy
Smashing <h5>
 
Posts: 1023
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   
But there isn't any whitespace in config, or any in the gotoURL function in functions.php, it worked fine before today.
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
Johnathan wrote:or any in the gotoURL function in functions.php, it worked fine before today.


It does not matter if it's in the function, as long as it's in the file or if there's any errors with the code, something will print.

We've told you yet and yet again that there's something getting printed before the headers and that's why your code fails.

Also, you should omit the closing tag (?>) for files being only contains configuration data and functions.
Andy
Smashing <h5>
 
Posts: 1023
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

   
I put the ?>'s in to see if it made any difference.

Just took all the function out of functions.php apart from the gotoURL one and I'm still getting it.
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
What is on line 1 of functions.php?

As Andy says, something must be being printed somewhere in your code, even if it is just a tab or a space you don't realise you have after the opening <?php tags in functions.php
Hey look! I made a blog! http://www.msinkinson.co.uk
User avatar
TT_Mark
Smashing <li>
 
Posts: 401
Joined: Tue Feb 10, 2009 6:27 am
Location: Sheffield, UK
   

   
Code: Select all
<?php
//Shortens and inserts shortened URL and original URL into database
function shortenURL($url,$custom){

first three lines of functions.php

Thinking of rewriting it all now anyway and getting everything I need in there from the beginning.
User avatar
Johnathan
Smashing <table>
 
Posts: 40
Joined: Sun Feb 08, 2009 4:04 pm
Location: Belfast, Northern Ireland
   

   
I have 2 solution for make this work:
    using ob_start() and in the end of script put the ob_flush()
    change your gotourl code with javascript location.href

Hope this can help
icreativelabs
Smashing <frame>
 
Posts: 13
Joined: Tue Oct 13, 2009 10:37 am
Location: Bandung, Indonesia
   

   
icreativelabs wrote:sing ob_start() and in the end of script put the ob_flush()


That would work but it would be better to find the actual cause of the problem rather than trying to hide it with a hack.
Andy
Smashing <h5>
 
Posts: 1023
Joined: Tue Sep 30, 2008 6:42 pm
Location: Sweden
   

Next

Return to Server-side Scripting



Who is online

Users browsing this forum: No registered users and 1 guest