简体   繁体   English

PHP标头网址重定向

[英]PHP header url redirect

I've searched around the inter webs and have been unable to find a solution for my problem. 我已在互联网之间进行搜索,但无法找到解决我问题的方法。 Here is my problem, I have a post script that enters the user's post and user id in the database. 这是我的问题,我有一个发布脚本,可以在数据库中输入用户的发布和用户ID。 What I want to do is from different pages on my web site, after it posts, to redirect back to the page that the user was just on. 我要做的是在网站发布后从网站上的其他页面重定向到用户所在的页面。 And all of it will take place after the user has logged into the website, so it's not a way to redirect to the login page, but back to the page the user was on after submitting a post. 而且所有操作都将在用户登录网站后进行,因此这不是重定向到登录页面的方法,而是返回到提交帖子后用户所在的页面的方法。 The problem is it goes to profile_test2.php even though I posted from homepage.php. 问题是,即使我从homepage.php发布,它也会进入profile_test2.php。

Here is my code from my form: 这是表格中的代码:

<form name="form" id="form" method="POST" action="add_post.php?bev=1">
<input type="text" name="textbox" id="textbox" spellcheck="true">
<input type="submit" value="submit">
</form>

And here is the code in the php script: 这是php脚本中的代码:

// Get the page the user posted from
$page = $_GET['bev'];

//Redirect this user to the page that displays user information
if ($_GET['bev'] == '1') {
$url = 'homepage.php';
} else if ($_GET['bev'] == '2') {
$url = 'profile_test2.php';
} else {
$url = 'profile_test2.php';
}

header('Location: '. $url);
exit();

Thank you in advance! 先感谢您!

Can't have a form action with a $_GET url. 不能使用带有$_GET网址的表单action You can use an <input type='hidden' name='bev' value='1' /> though. 不过,您可以使用<input type='hidden' name='bev' value='1' /> Since your method is $_POST I would use $_POST['bev'] . 由于您的方法是$_POST我将使用$_POST['bev']

+1 for Pekka's comment on using invisible element in the form instead of having GET variable passing through POST form. +1表示Pekka在表单中使用不可见元素而不是让GET变量通过POST表单进行评论。

<input type="hidden" name="bev" value="1">

If the page always redirect to profile_test2.php most probably the GET function is not working and it always falls under ELSE condition in the php script. 如果页面始终重定向到profile_test2.php,则最有可能是GET函数无法正常工作,并且它始终处于php脚本中的ELSE条件下。 You may wanna echo the $url just to double check on this. 您可能想echo the $url只是要仔细检查一下。

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

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