user1592953
user1592953

Reputation: 135

Redirecting users with PHP

I originally was redirecting users using headers with PHP. On my server though, I get a problem because HTML comes before the Location headers. Now, changing my script structure is not something I can do. What other ways for redirecting in my PHP can I use.

I tried these options, but they did not work:

echo "<META HTTP-EQUIV="Refresh" Content="1; URL=index.php">";

Also with single quotes:

echo <META HTTP-EQUIV="Refresh" Content="1; URL=index.php">;

Neither ways worked. What can I do?

Upvotes: 0

Views: 865

Answers (5)

geekman
geekman

Reputation: 2244

Sorry, I didnt realise the mistake you made in the echo code, so I edited the answer, check it out, it should work now.

Upvotes: 0

geekman
geekman

Reputation: 2244

And there is one major fault in your php try this

echo '<META HTTP-EQUIV="Refresh" Content="1; URL=index.php">';

Because you are already using the double quotes inside Meta tag, you must use single ones for starting and ending echo string. There you go., the Meta tag will work properly now.

Using header

header('Location:http://www.mtrix.in');

exit();

The person will be redirected to this location provided. But this has to be sent before any output, or even headers are sent, because headers can be sent only once, and are generally sent before output.

You can also output the following

<script> 

window.location='index.php';

</script>

The meta tag should work

<meta http-equiv="refresh" content="2;url=http://mtrix.in/">

Upvotes: 1

xdazz
xdazz

Reputation: 160833

If you want to show some html content, and after some time redirect to other page. You could use the below meta tag.

<meta http-equiv="refresh" content="5;url=http://www.example.com/otherpage">

Upvotes: 0

Gung Foo
Gung Foo

Reputation: 13558

Take a look at Output Buffering.. You basically call the ob_start() funciton before your first output and ob_flush() after the last output.

Upvotes: 1

Brad
Brad

Reputation: 163291

You can enable output buffering. The output will be buffered until it is complete, allowing you to set headers at any time.

Really though, you should fix your application so it isn't setup the way you describe. I know you say it is not something you can do, but if you are mixing your application logic with your HTML, there are other problems you will likely run into. Readability and reusability of your code is one.

Upvotes: 3

Related Questions