How to redirect users to a page in PHP

Read {count} times since 2020

If you want to redirect users to a specific page or url like when user is not logged in or something in PHP then you can accomplish this with header function or by Javascript printed by echo function.
The following example will redirect user to

Or you can use the normal Javascript :

window.location=’’;"; ?>

What’s the difference ?

header function is called in server itself not when the page reaches the browser. So no content will load, instead the client’s page will be redirected to the specified url.

Javascript redirection takes place in browser. So when the page loads to the Javascript redirect call the user will be redirected. It takes some time if you put the redirection code below script loading or stylesheet loading.

So it’s better to use header than calling a redirection in Javascript. If header function won’t work then you can use Javascript redirection. It’s that simple.

Show Comments