简体   繁体   中英

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. 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.

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:

// 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. You can use an <input type='hidden' name='bev' value='1' /> though. Since your method is $_POST I would use $_POST['bev'] .

+1 for Pekka's comment on using invisible element in the form instead of having GET variable passing through POST form.

<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. You may wanna echo the $url just to double check on this.

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