Jump to content

$_SERVER['HTTP_REFERER'] is there a better solution??


New Coder

Recommended Posts

Hello all,

 

I have been looking into using $_SERVER['HTTP_REFERER'] to populate the href of a back link.

 

I thought this would be a good idea as I have a login page that can be accessed from pretty much every page on my site. I wanted the user to be able to go back to the page they last viewed before they logged in.

 

eg.

The user has browsed to the gallery page as a guest. They then decide they wish to upload an image. Uploading images can only be done by a logged on user. So they click the login link at the top of the page. They login, a message is displayed saying welcome and they are presented with a back link or button.

 

So I was thinking:

 

<form action="<?php echo $_SERVER['HTTP_REFERER']; ?>"><input type="submit" value="Back" /></form>

 

or

 

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Back</a>

 

Is there a better way?

 

Many Thanks

The only other possible idea is to pass the previous URL via sessions, cookies or URL. HTTP_REFERER is sometimes reconfigured in the browser to pass different address than it is supposed to, but actually you should not be worried about that. If someone has such stupid ideas as using the protocol fields for different purposes, he should not be surprised that something does not work for him.

is there a better solution??

 

Yes, think of the log in as a 'box' or a code block that you include on any page that needs it (such as the way this forum handles the log in.)

 

If the visitor is not logged in, the log in form is displayed in a specific region on the page. The log in form submits back to the current page. The code that processes the log in form is part of the code block that you included on the page. If you are logged in, the username and a log out link/menu is displayed instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.