1

How can I get the previous viewed page title? For example, I visited node/21 then, I navigate to node/22, then in node/22 I'd like to get the node/21's title. In node/22, there's a text field where the default value is the node/21's title. Is there any function I can use for this? Please help as I badly needed this. Thank you.

2

You can use $_SERVER['HTTP_REFERER'] with PHP to get the URL of the last page you were on. This is not a reliable solution because not all user agents support it. plus users can manually intervene with the previous page and mess things up.

If a client side solution is sufficient for you, you can use javascript's document.referrer.

You could use a querystring as well and append that to the URL and read it from the page you're going to.

website.com/secondPage?refer=lastPageIWasOn

// then grab the value from the second page
$last_page_value = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';

// or use drupal_get_query_parameters()

// Then do something with it

Or you could use cookie/session to do this

if (!isset($_SESSION)) {
    session_start();
}
if (!isset($_SESSION['referrer'])) {
    $_SESSION['lastpage'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
}

// second page
$referer = isset($_SESSION['lastpage']) ? $_SESSION['lastpage'] : '';

This is a very common use case and StackOverflow has several questions on it with PHP. I suggest googling it. Translating it to drupal will depend on what you're doing with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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