There has to be something that's before that statement. Do you have any includes, or anything that would produce header information?

Otherwise maybe you could do a redirect with javascript like this:

<?php
	if(!$cxn = mysql_connect("localhost", "username", "password")){
		echo "<script type=\"text/javascript\"> location.href = \"http://www.example.com\";</script>";
	}

?>

    Nope, nothing before that statement, this would work in ASP using the alternatives (I'm a long experienced ASP developer) so unsure why its not working in PHP!

    I guess I could try the Javascript version but would prefer the serverside redirect, any chance you could experiment with your code example to see if you can get any success?

    Really appreciate your help!

      I put this on my server and it ran successfully. Try using JUST this and see if it works. If it does then there must me something in your code that is causing a problem, if not, then I don't know what to suggest.

      <?php
      	if(!$cxn = mysql_connect("localhost", "username", "password")){
      		header("Location: http://www.google.com");
      	}
      
      ?>
      

      EDIT:
      Maybe post some of your code so that I can see what's going on.

        Can you post the exact error message for us? Also check that there isn't anything before the '<?php' tag - a single space or blank line is counted as data and will cause PHP to send headers before the header() call.

          Hi mate,

          You're absolutely right, sorry I didn't test the original script on my live server, just on my local IIS5 server. It works fine online but doesn't work via my local server, I get this...

          Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'localhost' (using password: YES) in \Website Root\test.php on line 2

          Warning: Cannot modify header information - headers already sent by (output started at ...\Website Root\test.php:2) in ...\Website Root\test.php on line 3

          I'd really like to get my dev server setup correctly, do you know of any way to get it working in the same way as my online server?

          Cheers, @sh

            Right, because first the DB connect fails, and since you have display_errors set to On, PHP outputs the warning message.

            This shouldn't be an issue on your production server, since display_errors should never be enabled there. If you wanted to test it on your production server, you could add the '@' error suppressor before the mysql_connect() function so that PHP won't output that first warning message.

              bradgrafelman;10878721 wrote:

              Right, because first the DB connect fails, and since you have display_errors set to On, PHP outputs the warning message.

              This shouldn't be an issue on your production server, since display_errors should never be enabled there. If you wanted to test it on your production server, you could add the '@' error suppressor before the mysql_connect() function so that PHP won't output that first warning message.

              Ahhhh, so the debugging on my Dev server writes the errors as part of the 'page' stream, therefore breaking any code reliant on no headers being written, such as the redirect?

              I getcha, thanks for clearing that up, I appreciate your help.

                Just for anyone else's reference, I've written a script that checks whether the conn.php file is being referenced via a local OR an online server, and to avoid local errors only checks whether the link has been successfully established if the page is being viewed via the Live server.

                Also allows you to use different SQL auth depending on whether you're connecting to your local or remote server. Its not lightening science to anyone, but may be useful to any newbies like me browsing for a solution.

                Thanks to those above for their help in building this.

                <?PHP // ASH: Define the database connection string

                if ($_SERVER["HTTP_HOST"] == "liveserverURLhere.com") {

                if(!$conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD")){
                header("Location: offline.php");
                }
                $db = mysql_select_db('NAMEOFDB',$conn);

                } else {

                $conn = mysql_connect('localhost','root','password');
                $db = mysql_select_db('NAMEOFDB',$conn);

                }

                ?>

                  ashleyhall;10878720 wrote:

                  Hi mate,

                  I'd really like to get my dev server setup correctly, do you know of any way to get it working in the same way as my online server?

                  Cheers, @sh

                  Just for any fellow newbies with a similar problem - I was getting a 'Headers already sent' error on my local server, but not on my remote server - It seems that this was caused by literally ONE single blank line at the top of my PHP file, I've moved up the PHP tags now and the error is no longer occuring, and my redirect works.

                  Perhaps the more experienced here could confirm whether there is a PHP.ini setting perhaps to resolve this so as to match my online server configuration?

                    There's no configuration or anything similar to change; if there's a blank line that gets outputted, then that's the problem. You can't output any data before a header() call.

                      But for some reason my online server disregards any such space, as ASP does too - there must be a configuration setting for this?

                      The same page works fine online, but not locally unless I close up the gap!?

                        Looks like I was wrong; the output_buffering directive buffers not only data echo'ed within PHP tags, but also outside them (e.g. blank lines at the top). The default setting appears to be 4096, which means PHP will buffer 4KB of data before automatically sending headers. This is probably how your production server is setup.

                          Write a Reply...