Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Return to previous page type function

    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:
    PHP Code:
    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
    Last edited by fl00d; 04-21-2008 at 02:03 AM.

  • #2
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    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 Code:
    <?php
    /* This is at the top of the page */
    session_start();
    $_SESSION['return'] = "page_23";
    ?>
    mailbox.php:
    PHP Code:
    <?php
    session_start
    ();
    if(
    $_SESSION['return'] == "page_23") {
    $link_back "index.php";
    }
    ?>
    ...
    <a href="<?php print $link_back?>">Return</a>
    Last edited by BWiz; 04-21-2008 at 03:19 AM.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    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.

    PHP Code:
    <?
    session_start
    ();
    if (!isset(
    $_SESSION['return'])) {
      
    $_SESSION['return'] = $page;
    }
    ?>
    Chuck Norris counted to infinity.
    Twice.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •