如果用户未成功提交表单如何重定向
我的网站上有一个评论表。用户提交表单后,他/她将被重定向到“谢谢”页面。我希望只有在成功提交表格后才能访问感谢页面。如果用户在未先提交表单的情况下尝试访问感谢页面,我希望将用户重定向到主页。关于最好的方法有什么建议吗?
更新:我在使用下面的解决方案时遇到的问题是,即使用户正确提交了表单,用户也会被重定向到主页。
表单页面 PHP:
<?php
if (!isset($_SESSION)) {
session_start();
$_SESSION['sendMessage'] = true;
}
谢谢页面 PHP:
<?php
if (!isset($_SESSION['sendMessage'])) {
header('Location: http://www.example.com/index.php');
exit;
}
?>
I have a comments form on my site. After the user submits the form, s/he will be redirected to a "thank you" page. I would prefer that the thank you page only be accessible if one has successfully submitted the form. If the user tries to access the thank you page without first submitting the form, I would like the user to be redirected to the home page. Any suggestions on the best way to do this?
UPDATE: The problem I am having with the solution below is that the user is redirected to the home page even if that user correctly submits the form.
Form page PHP:
<?php
if (!isset($_SESSION)) {
session_start();
$_SESSION['sendMessage'] = true;
}
Thank you Page PHP:
<?php
if (!isset($_SESSION['sendMessage'])) {
header('Location: http://www.example.com/index.php');
exit;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您自己发布了解决方案:在会话中存储一个值。
[编辑]
马里奥是对的。您还需要在“谢谢”页面中使用 session_start() 。
You posted the solution yourself: store a value in the session.
[edit]
Mario is right. You'll need session_start() in the Thank You page too.