简体   繁体   中英

Redirect user on last page ( or page he came from ) PHP / JS

in administration i have simple list of users ( 1 line = 1 user ). And as it used to be I have a button for "EDIT" and button for "DETAIL". And here it is how each line looks like:

在此处输入图片说明

When user click on detail or edit it redirect him on new address (something like detail/24 (where 24 is ID) or edit/24

Question is:
1# How can I redirect user from detail to page he came from ( because of paginator it is important to redirect on specific page like: list.php?page=4

2# How can I redirect user from edit if he press something like SAVE which basicly reload edit page so he is not able to store it anywhere.

In another words for #2, user is on page list.php?page=4 and click EDIT so he get to something like /edit/32 on that page he make some changes and press SAVE button. When he click this button PHP send FORM and refresh page.

Is there a way to redirect user on page list.php?page=4 without knowing if he press SAVE or not?

3# Is there some other way how to redirect on last page? which simulate "GO BACK" button in browser?

2# Location.reload()

location.reload();

1# and 3# Window.history

<button onclick="goBack()">Go Back</button>

<script>
     function goBack() {
         window.history.back();
     }
</script>

If you want to redirect before rendering the HTML, I sugest you use header with $_SERVER global variable.

if($condition === true)
header('Location: '.$_SERVER['HTTP_REFERER']);

$_SERVER['HTTP_REFERER'] ( http://php.net/manual/en/reserved.variables.server.php ) returns the url of the page the user comes from.

Please note header function will only works if there no output before the call.

Hope can help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM