Using the Header function

Hi

This should be very simple but it is just not doing what I expect it to do.

I cannot get header to redirect to another page.

I even just set up the simple file below and it still does not work.

I’m setting up a / sign up login system and it all works fine apart from this.

Any ideas anybody.

Thanks


<?PHP
    header ("Location: showmessage.php");
?>

Make sure that you heave nothing being output prior to the header call and use the FQDN to the location you with redirect to. :slight_smile:


<?php
header('Location: http://www.example.org/showmessage.php');
exit;

Thanks for replying.

I had already tried both of those suggestions but still no luck.

Any other ideas please.

<?PHP
	
    header("Location: http://www.justinoakleyandassociates.co.uk/testing/login/showmessage.html");

	exit;
	
?>

Do you receive an error? Is the the complete content of the script? For instance…

Are you maybe saving the file as UTF-16 with BOM, so the byte mark is output and subsequently PHP won’t redirect anymore?

I am getting the following error message.

Warning: Cannot modify header information - headers already sent by (output started at /content/HostingPlus/j/u/justinoakleyandassociates.co.uk/web/testing/login/header.php:3) in /content/HostingPlus/j/u/justinoakleyandassociates.co.uk/web/testing/login/header.php on line 11

<?PHP

	error_reporting(-1);
	
	ini_set('display_errors',true);
		
    header("Location: http://www.justinoakleyandassociates.co.uk/testing/login/abc.html");
	/* Redirect browser */

	exit;
	
?>

I have been trying out various ideas suggested by other forums

and not being conclusive, white space after the ?> seems

to have lots to do with it although including it and excluding it seems

to have variable results.

The jury is out.

What’s on line 3 in that file?

Because the error says that the header() call is on line 11, so there must be something before the opening <?php tag.

The whitespace after the closing tag shouldn’t make much difference as the script will never reach that point ('cos of the exit).

Is this file being included by a different file?

If you can post everything above that line here, we’ll be able to point to what’s causing the problem.

If you’re executing a login query and outputting anything (eg an error, or whitespace) before the header redirect then you’ll get this error.

There’s no real reason for the error unless there are other scripts above it on the page, or if you are including it in another file. Having stuff below it will have no effect on the error (or it shouldn’t do!).

So, in your php file on line 1 is it exactly what you posted?

<?php header("Location: http://www.justinoakleyandassociates.co.uk/testing/login/abc.html"); ?>

Cannot modify header implies that something else was output before the header. By the looks of the error it was something on line 3

Have you tried moving the header to line 1?

I have managed to get it to work consistently but I’m not sure which of the changes I made did the trick.

I took out all blank lines and white space at the end and it was fine.

I was always aware that the Header function must not be preceded by any output to the browser and that was never the problem.

Thanks everybody for your ideas, I learnt a lot in the process.