Is it possible to redirect a user to a different page through the use of PHP?
Say the user goes to www.example.com/page.php
and I want to redirect them to www.example.com/index.php
, how would I do so without the use of a meta refresh? Possible?
This could even protect my pages from unauthorized users.
you can update the header in php: header
I've already answered this question, but I'll do it again since in the meanwhile I've learnt that there are special cases if you're running in CLI (redirects cannot happen and thus shouldn't
exit()
) or if your webserver is running PHP as a (F)CGI (it needs a previously setStatus
header to properly redirect).I've also handled the issue of supporting the different HTTP redirection codes (
301
,302
,303
and307
), as it was addressed in the comments of my previous answer, here are the descriptions:We can do in two way
by below php code
<?php header("Location: https://bskud.com/PINCODE/BIHAR.php"); exit; ?>
Save Above code in https://bskud.com/PINCODE/BIHAR/index.php
2.When any condition true then redirect to other page
`
If you're running on Apache you can also use .htaccess for redirect.
In the eve of the semantic web, correctness is something to consider. Unfortunately, PHP's "Location"-header still uses the HTTP 302-redirect code, which, strictly, isn't the best one for redirection. The one it should use instead, is the 303 one.
W3C is kind enough to mention that the 303-header is incompatible with "many pre-HTTP/1.1 user agents," which would amount to no browser in current use. So, the 302 is a relic, which shouldn't be used.
...or you could just ignore it, as everyone else...
or if you have already opened php tags, use this:
You can also redirect to external pages, eg:
Make sure you include
exit()
orinclude die()