+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    prestigeplus is offline x10Hosting Member prestigeplus is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    7

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

    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)?

  2. #2
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

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

    Simply use the ternary operator when defining the variables and check the isset:
    PHP Code:
    # ...
    $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 by as4s1n; 04-30-2010 at 12:04 AM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  3. #3
    prestigeplus is offline x10Hosting Member prestigeplus is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    7

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

    Quote Originally Posted by as4s1n View Post
    Simply use the ternary operator when defining the variables and check the isset:
    PHP Code:
    # ...
    $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!

  4. #4
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

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

    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 Code:
    # 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 Code:
    # 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 by as4s1n; 04-30-2010 at 11:07 AM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

+ Reply to Thread

Similar Threads

  1. Site down / redirected to x10hosting home page
    By benjbenj in forum Free Hosting
    Replies: 3
    Last Post: 04-04-2010, 05:24 AM
  2. My Web site redirected to x10hosting home page
    By bijosoft in forum Free Hosting
    Replies: 6
    Last Post: 04-03-2010, 05:23 PM
  3. not viewing the page
    By eiitmgwl in forum Free Hosting
    Replies: 1
    Last Post: 02-12-2010, 09:38 AM
  4. Replies: 1
    Last Post: 02-01-2010, 11:17 PM
  5. Replies: 4
    Last Post: 02-20-2008, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers