简体   繁体   English

PHP 如何通过 POST 发送 SAFE url 以便在 php 脚本完成后它可以重定向回它来自的页面?

[英]PHP How can I send a SAFE url via POST so that after a php script has finished it can redirect back to the page it came from?

What is the best way to go back to a page where a form was submitted?将 go 返回到提交表单的页面的最佳方法是什么?

For example, if i send some data via POST to "save-data.php" and that file in turn uploads the posted data to a database how can i then return to the page that initially sent the POST data if the url is not known?例如,如果我通过 POST 将一些数据发送到“save-data.php”,然后该文件将发布的数据上传到数据库,那么如果 url 未知,我该如何返回到最初发送 POST 数据的页面?

I'm not sure I have explained right.我不确定我的解释是否正确。

MORE DETAIL:更多详情:

the data is posted by an admin user only.数据仅由管理员用户发布。 Im using this code for an editable navigation bar stored in a database.我将此代码用于存储在数据库中的可编辑导航栏。 The admin can edit the label and href then store it.管理员可以编辑 label 和 href 然后存储它。 The trouble is that it can be edited from any page in the site so long as you are logged in as an admin.问题在于,只要您以管理员身份登录,就可以从站点的任何页面对其进行编辑。 When you hit a save button (on the same page as the navigation bar) it posts the data to a php script which inserts/updates the database.当您点击保存按钮(在与导航栏相同的页面上)时,它会将数据发布到插入/更新数据库的 php 脚本。 I just need to send the user back to the page where save was clicked.我只需要将用户发送回单击保存的页面。

If this is dynamic, you could pass a hidden input value:如果这是动态的,您可以传递一个隐藏的输入值:

<input type="hidden" name="return" value="myfile.php" />

Not a fan of this as it can be changed by the user.不喜欢这个,因为它可以由用户更改。 But I do stuff like that in my applications a lot but its more like this:但是我在我的应用程序中经常做这样的事情,但更像是这样:

<input type="hidden" name="return_action" value="detailview" />

Then do a lot of stuff to validate the action and then permissions involved (larger conversation than this.)然后做很多事情来验证操作以及所涉及的权限(比这更大的对话。)

If this is a static return and you control it, you can simply do this:如果这是一个 static 返回并且您控制它,您可以简单地执行以下操作:

header('Location: myfile.php');
exit();

Just add that after your save and your fine.只需在保存和罚款后添加即可。

Edit Sorry just noticed if the URL is not known.编辑抱歉刚刚注意到 URL 是否未知。 Do you control all parts?你控制所有的部分吗? Which parts do you control and to what level?您控制哪些部分以及控制到什么级别? I am assuming your not just letting everyone post data to a URL?我假设您不只是让每个人都将数据发布到 URL?

You can do it with JavaScript:您可以使用 JavaScript 做到这一点:

window.onload = history.go(-1);

It's impossible to do in PHP.在 PHP 中是不可能的。 You need to know the referring URL to send someone there.您需要知道推荐人 URL 才能派人去那里。 $_SERVER['HTTP_REFERER'] may contain the referrer, but it's not guaranteed to be sent (some firewalls and browsers removes the referer header). $_SERVER['HTTP_REFERER']可能包含引荐来源网址,但不保证会被发送(某些防火墙和浏览器会删除引荐来源网址头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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