- PHP

How to Redirect in PHP without Header

When doing redirects in PHP, we usually use the header() function. I’m not going to lie, in most cases it’s just the best and safest way because it works on the server side.

But if for some reason we want to use an alternative, we can use solutions known from HTML and JavaScript. But let’s remember to avoid them in critical cases, because the user can, for example, remove a given line of code on the client side, or disable JavaScript in his browser (or not have it at all).

Redirecting with HTML:

<meta http-equiv="refresh" content="0; URL=https://snippetsdb.com">

in PHP code:

<?php

echo '<meta http-equiv="refresh" content="0; URL=https://snippetsdb.com">';

?>

Redirecting with JavaScript:

<script type="text/javascript">
    window.location.href = 'https://snippetsdb.com/';
</script>

in PHP code:

<?php

echo '<script type="text/javascript">window.location.href = "https://snippetsdb.com/"</script>';

?>