Page 1 of 2

Capture url 2 clicks back

Posted: Sun Aug 27, 2006 6:31 am
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

Posted: Sun Aug 27, 2006 6:56 am
by Chris Corbyn
You need to "track" link clicks by stacking them into a container in a session ;)

Posted: Sun Aug 27, 2006 3:41 pm
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

Posted: Sun Aug 27, 2006 3:47 pm
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);
}

Posted: Sun Aug 27, 2006 5:51 pm
by Ollie Saunders

Code: Select all

$_SESSION['page_trail'][] = $_SERVER['PHP_SELF'];
REQUEST_URI not better?

Posted: Mon Aug 28, 2006 11:13 am
by pickle
What about using javascript:

Code: Select all

history.go(-2);

Posted: Mon Aug 28, 2006 11:24 am
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?

Posted: Mon Aug 28, 2006 12:15 pm
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.

Posted: Mon Aug 28, 2006 12:24 pm
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.

Posted: Mon Aug 28, 2006 3:37 pm
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

Posted: Mon Aug 28, 2006 4:17 pm
by feyd
The "refresh" header is non-standard. Some servers and browser may choose to ignore it. Use a <meta> tag version instead.

Posted: Mon Aug 28, 2006 7:39 pm
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).

Posted: Tue Aug 29, 2006 6:08 am
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.

Posted: Thu Aug 31, 2006 12:49 am
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

Posted: Thu Aug 31, 2006 9:02 am
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.