是否有可能记住第一页的网址,将其用作返回第二页的链接 - PHP

Say for example I have one page and I click on a link for a next page. Is it possible to then remember the first page's url to use it as a link to go back to on the second page. I have the following code to get the url of the first page.

<?php
    $_SERVER['PHP_SELF'];
?>

I also have a link with the following code on the second page.

<a href='<?php $_SERVER['PHP_SELF'] ?>'>

But it doesn't seem to work, because it gets the url of the page that it's on.

How can I remember or store the url to use it on the second page.

On page 1 save the current URL in a session variable (you can read up on sessions here):

<?php
session_start();
$_SESSION['return_url'] = $_SERVER['PHP_SELF'];

On page 2 use that in your hyperlink:

<?php
session_start();
?>
<a href='<?php echo $_SESSION['return_url'] ?>'>

You'll want to escape the output on page two just in case someone is trying to XSS you.

<a href='<?php echo htmlspecialchars($_SESSION['return_url']) ?>'>

For the first page's link:

<a href="http://mywebsite.com/landingpage?redirect_uri=<?php echo urlencode($_SERVER['REQUEST_URI']);?>">Link text</a>

For the second page's link:

<a href="<?php echo urldecode($_GET['redirect_uri']);?>">Link text</a>

Alternatively, you can just do this on the second page (no need to use any special HREF for the first link anchor):

<a href="<?php echo $_SERVER['HTTP_REFERER'];?>">Link text</a>