PDA

View Full Version : Return to previous page type function


fl00d
04-21-2008, 01:57 AM
Hi,

I'm writing a return to previous page type function like phpBB, SMF, VB and the likes implement. Someone wants me to implement it into their PM system so it would work like so:

When the user first visits the site and logs in, if they have a new/unread PM, they will be redirected to a page that says 'You have a new PM | Read | Return'. They have the option to click read, which brings them to the PM, or the option to 'Return', which returns them to the page they were previously viewing or trying to view. This is the part I'm having trouble with, I don't know how to effectively capture the previous page. I've thought up a couple methods, but none are terribly efficient nor effective.

1) Use Javascript history.go(-1)
Problem: If they enter from another site (ex: google.com), they will be returned to Google. I don't want to kick them off my site...

2) Use HTTP_REFERRER
Problem: May not be stored or again could be another site, not part of my own. I could fix this by checking if their is a HTTP_REFERRER, and then preg_match or search it for my host name and if not found, set a 'static' page to go to.
ex:

if(empty(REFERRER)){
$url = 'mysite.com/members.php';
}else{

$find = '^mysite.com';
if(!preg_match($find,REFERRER)){
$url = 'mysite.com/members.php';
}
}
header("Location: $url");


Anyone have any better suggestions or how-to's?

Thanks
-fl00d

BWiz
04-21-2008, 02:23 AM
I don't think my method is very practical, but you could create a session variable, with each page having it's own unique identity. So, with the return option, it just looks at the session variable (which is redeclared on every page), and then matches the id too the correct link.

So something like:
inbox.php:

<?php
/* This is at the top of the page */
session_start();
$_SESSION['return'] = "page_23";
?>
mailbox.php:

<?php
session_start();
if($_SESSION['return'] == "page_23") {
$link_back = "index.php";
}
?>
...
<a href="<?php print $link_back; ?>">Return</a>

fl00d
04-21-2008, 03:01 AM
Actual your method makes a lot of sense. I'll admit, I did think of that, but I discarded it because if it is on every page, it would get overwritten. So when the time comes that the user is redirected to the 'You have a new message' page, the new URL would be the new message page... obviously, I just don't have to have new message page set a URL... silly me :P

Thanks :) I'll try it out.

Arnaud
04-21-2008, 10:41 AM
What about setting your $_SESSION variable only once?

That would make sure the user is redirected to the first page he viewed on your website.


<?
session_start();
if (!isset($_SESSION['return'])) {
$_SESSION['return'] = $page;
}
?>

kbluhm
02-09-2010, 01:35 PM
:confused::confused::confused::confused::confused:

MattF
02-09-2010, 04:13 PM
Why can't you just use a visible message notifier somewhere on the page or a javascript alert/overlay etc? I'd personally find it bloody annoying to get shunted to a notification page every time there was a message, as I guess will a notable percentage of your users, once the novelty wears off.