php - How to get the requested URl from previous page -
i have landing page in website, in each page have php code check cookies , if user has specific cookie remain @ page , if doesn't redirect landing page. on landing page have button when users click on it, cookie created.
when user clicks on button pages should goes previous page before. code is
if(isset($_cookie['username'])){ $_cookie['username']; header('location: http://example.com/main'); } else{ // }
is there anyway find previous page link or original requested url?
yes, can utilize http_referer
value found in $_server
superglobal:
if(isset($_cookie['username'])) { $_cookie['username']; // <-- for? doing it? header('location: http://example.com/main'); die(); } else{ header( 'location: ' . !empty($_server['http_referer']) ? $_server['http_referer'] : 'http://example.com/access_denied' // in case http_referer blank, fallback ); die(); }
note shouldn't depend on value strict security reasons, "bounce user previous page" function, should adequate! don't forget utilize exit
or die
statements after sending redirect headers or rest of script still execute!
note docs http_referer
:
the address of page (if any) referred user agent current page. set user agent. not user agents set this, , provide ability modify http_referer feature. in short, cannot trusted.
php url cookies
No comments:
Post a Comment