개발 꿀팁/PHP

[PHP] Back to previous page

Jammie 2018. 1. 16. 01:19
반응형

Let's look at how to go to the previous page in PHP.

There is, of course, a way to go back to the previous page in JavaScript.

JavaScript has a history built-in object, and there's a back() or go() function that saves the previous page information.

But is it possible to go to the previous page in PHP and server language?

This is also possible and you can go back using the information on the previous page.


※ How to go to the previous page of PHP


The first thing you need to know is that in order for the server to be able to move the page, there is no output on the web page. In other words, if there is no output to the browser, you can move and you need the following built-in variables to refer to the variable where the address of the previous page is stored.



 $_SERVER['HTTP_REFERER']


With these keywords, you can retrieve the page address just before you visit the current page. Save the loaded page to a variable and use location to move the page. See the example below.



※ Return to previous page View sample source code


Below is a simple example to go to the previous page. 

After saving the information of the previous page in the variable $prevPage, use header() to move it.


<? php

$prevPage = $_SERVER ['HTTP_REFERER'];

// Store previous page information in a variable


header('location:'. $prevPage);

// Move page

?>


It is now very easy to go back to the previous page. What if this code is used in JavaScript? It should look something like this:


<script>

   history.back();

</script>



Here's how to go to the previous page.

반응형