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.
header( 'Location: http://www.yoursite.com/new_page.html' );
You can use session variables to control access to pages and authorize valid users as well.
http://php.net/manual/en/reserved.variables.session.php.
Recently, I got cyber attacks and decided, i needed to know the users trying t access Admin Panel or reserved part of the web application.
so, I added a log access IP and user sessions in a text file because I don't want to bother my database.
You can attempt to use the php header function to do the redirect. You will want to set the output buffer so your browser doesn't throw a redirect warning to the screen.
Use
header()
function to send HTTPLocation
header:Contrary to some think,
die()
has nothing to do with redirection. Use it only if you want to redirect instead of normal execution.example.php:
Result or 3 executions:
Resuming — obligatory
die()
/exit()
is some urban legend, that has nothing to do with actual PHP. Has nothing to do with client "respecting"Location:
header. Sending header does not stop PHP execution, regardless of client used.