Capture url 2 clicks back

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Capture url 2 clicks back

Post by bob_the _builder »

Hi,

How can I capture and store a url (not in db) from 1-2 clicks previous then use header location to direct back to that page?

$HTTP_REFERER; can only go back 1 page?


Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to "track" link clicks by stacking them into a container in a session ;)
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Any examples?


Its basically so after a record is deleted, have it redirect them back to the page the record was on.


Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Not a very clean example but it should give you something to think on.

Code: Select all

<?php

session_start();

function stack_page()
{
    $_SESSION['page_trail'][] = $_SERVER['PHP_SELF'];
}

function get_prev_page($back=1)
{
    $trail = $_SESSION['page_trail'];
    if (isset($trail[count($trail)-1-$back]))
    {
        return $trail[count($trail)-1-$back];
    }
    else return false;
}

?>
So to go back two pages....

Code: Select all

if ($old_page = get_prev_page(2))
{
    header('Location: '.$old_page);
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$_SESSION['page_trail'][] = $_SERVER['PHP_SELF'];
REQUEST_URI not better?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What about using javascript:

Code: Select all

history.go(-2);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

seems to me that sending them "two pages back" is an unclean/hacky way of doing this anyway. Do you have more than one deletion page or something?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Javascript is not the best way, especially if the page was processed through a form post (as this will take you back to the then-current state of the page).

If this is your progression:
record_list -> edit_form -> edit_ok

Then you can pass the record_list page name to the edit form as a hidden field, then when the form is submitted, the record_list page name gets sent along with it to edit_ok, where you can redirect back to it from there.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Everah wrote:Javascript is not the best way, especially if the page was processed through a form post (as this will take you back to the then-current state of the page).
and it can go back to pages that aren't your site.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Basically its a photo gallery, there are options to delete gallerys, sub gallerys and images, they have a delete link above each, also with pagination.

If they choose to delete a photo it sends them to a page with the form and delete button, upon hitting the delete button it takes them to a process page that processs the delete function. 2 steps (pages)

1st page has all the forms for the functions and the 2nd page has all the functions to process the forms.

When its complete I want to use something like:

Code: Select all

header("Refresh: 3; url=index.php?action=the_page _they _came_from");
Not sure of the best way to go?


Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The "refresh" header is non-standard. Some servers and browser may choose to ignore it. Use a <meta> tag version instead.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

bob_the _builder wrote:Hi,

Basically its a photo gallery, there are options to delete gallerys, sub gallerys and images, they have a delete link above each, also with pagination.

If they choose to delete a photo it sends them to a page with the form and delete button, upon hitting the delete button it takes them to a process page that processs the delete function. 2 steps (pages)

1st page has all the forms for the functions and the 2nd page has all the functions to process the forms.

When its complete I want to use something like:

Code: Select all

header("Refresh: 3; url=index.php?action=the_page _they _came_from");
Not sure of the best way to go?


Thanks
Have you given my 'theory' a try? Seems like it would be most useful for a situation like yours (in fact, I know it would be, I have used it for the exact same situation as you).
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

I used Everah idea posting a hidden field:

Code: Select all

<input name="referer" type="hidden" value="'.$_SERVER["HTTP_REFERER"].'">
and feyd meta option:

Code: Select all

<meta http-equiv="refresh" content="3; url='.$_POST['referer'].'">

Thanks.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Actually the hidden field option didnt work very well now, as I added code to confirm deletion, Basically now $_Post['referer'] is non existant.


example:

Code: Select all

case 'delete_subcat':

	if (array_key_exists("confirm",$_REQUEST)) {

//Delete code here

		echo '<br />Sub category & images were successfully deleted<br /><br /><br />Please wait while you are redirected ...
		<meta http-equiv="refresh" content="3; url='.$_POST['referer'].'">';
		}else{
		echo 'Do you really want to delete this sub category and all its images?';
		echo '<br /><br /><a href="index.php?action=process&process=delete_subcat&subcat_id='.$_GET['subcat_id'].'&confirm=y">Yes</a> - <a href="#" onClick="history.go(-1)">No</a>';
}
 
break;
Would it be bad practice to post referer in the delete link and use $_GET to grab it after deletion has happened?

Is there a work around for this, would be nice to keep it simple as it would be without the deletion prompt in there.

The other option is to use the folowing code to confirm deletion:

Code: Select all

<input type="submit" value="Delete Category" onclick="return confirm(\'Are you sure you want to do delete this category?\')">
Is that fail safe across browsers?



Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, because not all browsers have Javascript enabled. There really is no reason why you can't carry vars from form to form using either hidden post vars or sessions.
Post Reply