1

I'm hoping someone could help me finish off some php code (the avon guy already kindly helped me with this but I'm still struggling with the last bit).

All it is, is I have a form where I have 10 particular sequences of digits, which if entered, allows the form to redirect to the following page. If anything else is entered I want the page to deny access with some kind of error prompt.

At top of the php, in the part before any php is printed, avon guy suggested an array to check the 10 correct sequences against.

$possibles = array('rva858', 'anothersequence', 'andanother'); 
$match = $_POST['nextpage']; 
if (array_search($match, $possibles) != false) { 
    //match code in here 
} else { 
    // fail code in here 
}

I'm not sure what to put in the //match code in here AND the //fail code in here, bits. Can someone help me with this last bit please?

Many thanks

Jon

5
  • 3
    The door to door cosmetics salesman helped you with php? Commented Jan 13, 2016 at 23:10
  • 1
    Just an FYI, your array_search will fail if the value you enter is the first one in the $possibles array. array_search will return the key, if the key is 0, then it is ==false. You should use in_array which will return true/false if the value is in the array. No odd conditions to look for. Commented Jan 13, 2016 at 23:19
  • @Steve I wondered the same, but I think it was this guy: stackoverflow.com/users/1125416/the-avon-guy Commented Jan 13, 2016 at 23:22
  • Wouldn't you just put your protected code in the "match code in here" spot, and your access-denied error in the "fail code in here" spot? Commented Jan 13, 2016 at 23:25
  • Ha! @Theavonguy lives in the same town as me - maybe he actually has knocked on my door! Commented Jan 14, 2016 at 0:53

1 Answer 1

2

If you are just trying to redirect to another page using php, you can use header('Location: mypage.php');. More information on header here.

So for your code example (edited based on comment):

invitation.php

<?php
//invitation.php
$possibles = array('rva858', 'anothersequence', 'andanother');
$match = $_POST['nextpage']; 
if (array_search($match, $possibles) === false) 
{  
    //If fail
    header('Location: formpage.php?errorMessage=Incorrect code!');
    exit();
} 
//If success:
//All of the invitation.php html and success code below

formpage.php

<?php
//formpage.php
if(!empty($_GET['errorMessage'])){
    echo '<span>' . $_GET['errorMessage'] . '</span>';
}
?>
<form action="invitation.php" method="post">
    <input name="rsvp" type="text" />
    <input type="submit" value="Submit" name="submit" />
</form>
Sign up to request clarification or add additional context in comments.

5 Comments

Hey thanks! I think it's slightly more complicated than that. I have a form on that page that contains this <form action="invitation.php" method="post"><input name="rsvp"> (This is a wedding invite system). I've given all my guests codes. Currently when they submit them in the form, if the code is right it loads up the correct page (within an iframe in invitation.php). If it goes wrong they get a dead page. Instead of a dead page I want the array to be checked when they press 'submit'. If it's correct, allow the redirection to invitation.php if not, echo an error or forward to a retry page.
Hey @Jon , you can still do what I think you're wanting with the code above. With the else (if the match fails), you can redirect back to the form page again and pass a query parameter. Example: header('Location: formpage.php?errorMessage=message'); Replace message with whatever error message you want. Then on your form page, you will use $_GET['errorMessage'] to grab the message from the URL. More on $_GET here php.net/manual/en/reserved.variables.get.php . I've also updated up answer above to help a bit.
Hey Michael, thank you so much. I'm so close, (with your help I might add). I've done all you've suggested and now the formpage.php will echo an error message from invitation.php, but that's all it does. It doesn't seem to allow for a correct code such as 'rva858' despite it being in the array. Is it something I need to edit with the 'nextpage' bit you've put in inverted commas? Maybe I should point out that the code entered, goes to load up a subdomain, ie rva858.anrsvp.com within an iframe in invitation.php. Sorry, I hope that makes sense! :)
Not to worry, I figured it out :) $match = $_POST['nextpage']; just needed to be $match = $_POST['rsvp']; to match the name of the field on the previous page. All working perfectly now, thanks! :D
Awesome @Jon glad you have it figured out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.