Reputation: 3141
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;
}
?>
Upvotes: 0
Views: 183
Reputation: 116110
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.
Upvotes: 1