I would like to get the ID (or permalink) of the page the visitor visited just before visiting the current page - in other words the ID of the last page in the browser history.

Can this be done? Any idea how to do it?

  • Search inside the print_r( $GLOBALS ); array. Not sure right now, but it maybe inside $_GET/_REQUEST/_SESSION/_SERVER;...
    – kaiser
    Commented Dec 7, 2011 at 16:42
  • @kaiser cheers, there is indeed a HTTP_REFERER in _SERVER - giving me the URL of the previous page. Perfect!
    – ptriek
    Commented Dec 7, 2011 at 16:50
  • This might be useful: codex.wordpress.org/Function_Reference/wp_get_referer Commented Apr 10, 2015 at 15:37

2 Answers 2


Break this down into two parts:

First, we create a variable that stores that last-visited page URL, like this:

$prev_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

Then, you could either use substr and strpos to trim down everything between ?= and the / after the ID number. like this:

$prev_url = 'http://www.yoursite.com/?p=123';
$id_block = substr($prev_url, strpos($prev_url, "?p=")+1);
$id = substr($id_block, 0, strpos($id_block, "/"));

.. Or, you could use jQuery/Javascript to achieve the same.

I haven't tested this but it should work - let me know how it does! Good luck ;)

  • thanks - i was already thinking in this direction, but php manual sais about this: "The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted." So I'm a bit in doubt whether it's the best way to go...
    – ptriek
    Commented Dec 7, 2011 at 16:56
  • True - the HTTP_REFERER depends on the server alone. To be honest, I am unaware of any other solutions for this. If this is not a crucial function, I would go ahead and use HTTP_REFERER as it is pretty stable from agent to agent.
    – cr0z3r
    Commented Dec 7, 2011 at 17:19
  • It's not a crucial function, so I'll just go with this & provide a fallback scenario.. Thanks for your help!
    – ptriek
    Commented Dec 7, 2011 at 17:31

I've done this trick like this to allow me to also check if it is one of my pages :

//Get the last page link from history

$prev_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;

//Check if not null and if it contains my blog url

if( !empty( $prev_url ) || strpos( $prev_url, (string)get_blog_details()->domain ) !== false ) :
    //Then display it only if it's one of my blog page ?>
    <a href="<?php echo $prev_url; ?>" class="previous-history-link">The last page from history</a>
<?php endif; ?>
  • 3
    Just a sidenote here, HTTP_REFERER is unreliable as this can be turned of by the end user, so never rely on that Commented Mar 2, 2015 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.