How can I stop a user from viewing a page unless they are redirected there?

prestigeplus

New Member
Messages
7
Reaction score
0
Points
0
I have a page that sends an email with data retrieved from the previous page's POST data. The problem is, if the user visits the email page directly, it will send an email but with blank variables. Is there a way to restrict access to the page so you can only send the email by clicking the form submit button?

If there is a way, people can just hit back and submit again, which leads me to another question. Can I limit the amount a user can submit that form to say...2 times per hour every 2 hours (Ex: Submit 2 times at 1:05 P.M., wait until 3:05 P.M. to submit again)?
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Simply use the ternary operator when defining the variables and check the isset:
PHP:
# ...
$var1 = (isset($_POST['varName1'])) ? $_POST['varName1'] : ''; # Define var 1
$var2 = (isset($_POST['varName2'])) ? $_POST['varName2'] : ''; # Define var 2
# Etc.

# Check if the value is empty
if(empty($var1) || empty($var2)) {
     echo "Empty fields, please <a href='pageBeforeThis'>Form</a>";
     exit;
}
# ...

That way it echos a link if it is blank that sends the person back to the form and stops the function.
 
Last edited:

prestigeplus

New Member
Messages
7
Reaction score
0
Points
0
Simply use the ternary operator when defining the variables and check the isset:
PHP:
# ...
$var1 = (isset($_POST['varName1'])) ? $_POST['varName1'] : ''; # Define var 1
$var2 = (isset($_POST['varName2'])) ? $_POST['varName2'] : ''; # Define var 2
# Etc.

# Check if the value is empty
if(empty($var1) || empty($var2)) {
     echo "Empty fields, please <a href='pageBeforeThis'>Form</a>";
     exit;
}
# ...

That way it echos a link if it is blank that sends the person back to the form and stops the function.

Thank you!
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
I'm sorry, I forgot something. I realise that if you have say 4 or 5 or more variables that the if statement may get cluttered,
PHP:
# Bad:
if(empty($var1)||empty($var2)||empty($var3)||empty($var4)||empty($var5) #etc.)
you may choose to run a foreach loop and check each. But for this to work you will have to store the variables in an array (which you should do anyway):
PHP:
# note format for array 'varName'=>'value' and is referenced on the page as $arrayName['varName'];
$vars = array(
'var1'=>$_POST['varName1'], 
'var2'=>$_POST['varName2'],
'var3'=>$_POST['varName3'],
'var4'=>$_POST['varName4']); # Add/subtract as necessary
$errors = array(); # Array of errors for easy display

# Loop each value of the array
foreach($vars as $values) {
      # If it is empty tell them (empty checks for strLen($var) == 0 as well)
      if(empty($values) || !isset($values)) {
            $errors[]= 'Empty value';
      }
}

# ...

# What to display if errors
if($errors) {
          echo "Errors: <ul><li>";
          echo implode("</li><li>",$errors);
          echo "</li></ul> <a href='linkBackToForm'>Try again</a>";
          exit;
}
#...

That way it runs the loop through every value and checks with one simple if() statement. Much more efficient too.
 
Last edited:
Top