php - How to get the requested URl from previous page -


I have a landing page in my website, in every page I have a PHP code that will check the cookies and if User has a specific cookie, it will remain on that page and if it does not redirect to the landing page. I have a button on the landing page, when the user clicks on it, the cookie will be created.

When the user clicks the button, the page should first return to the page that was first. My code is

  if (isset ($ _COOKIE ['username'])) {$ _COOKIE ['username']; Headers ('Location: http://example.com/main'); } Else {//}   

Is there any way to find the previous page link or the original requested URL?

Yes, you can be found in the HTTP_REFERER value:

  if (isset ($ _ cookie ['username'])) {$ _COOKIE ['username']; // & lt; - what is this? What are you doing with it? Headers ('Location: http://example.com/main'); Die (); } Other {header ('location:'. ($ _ Server ['HTTP_REFERER']) $$ _SERVER ['HTTP_REFERER']: 'http://example.com/access_denied' // If the HTTP_REFERER is empty, for ); Die (); }   

Note that you should not rely on this value for strict security reasons, although, as a "bounce to the user on the previous page", that should be enough! Do not forget to use the exit or die statement after sending the redirect headers or your script will still be executed!

Note from Docs HTTP_REFERER :

The page's address (if any) that refers to the user agent on the current page. It is not set by the user agent, all user agents will set it, and some provide the ability to modify HTTP_REFERER as a feature. In essence, it can not be really trusted.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -