Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Stupid Question- how to assign more than 1 result to a variable

    Hello everyone,

    I am trying to create a custom captcha form for my site that uses very particular answers. I am only going to have 1 answer to the question (Alabama), but I want several results to break the captcha (Al, AL, al, alabama, etc). Can someone help me alter this line of code to perform like I envision.

    Here is what I have right now:

    PHP Code:
        $captcha_exp '/Alabama/'

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,323
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    You can't assign more than one result to a string variable. Not in the way you're talking of anyway.

    You could however have them seperated by commas:
    $captcha_exp = 'Alabama,AL,al';

    Then use the explode() function to split them into an array. Thats hard work though when you could just use an array straight away:
    $captcha_exp[] = 'Alabama';
    $captcha_exp[] = 'Al';
    $captcha_exp[] = 'al';

    With an array you can then use in_array() to check if a value is in it.

    You can look up those functions by going to www.php.net/<function_name> and reading the comments on how to use them.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    sakina (11-07-2012)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I ended up using an array and the in_array() function to set each variable as a legitimate response, and it now appears to be working exactly like I was hoping.

    Thank you very much for sending me in the right direction.

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    My code if it helps any future readers:

    PHP Code:
        $captcha_exp = array ("Alabama""AL""al""Al""alabama");

         if (
    in_array("/Al/"$captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";
    }
     if (
    in_array("/Alabama/"$captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";
    }
     if (
    in_array("/AL/"$captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";
    }
     if (
    in_array("/al/"$captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";
    }
     if (
    in_array("/alabama/"$captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";


  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Please disregard my success post. While I am able to submit to my form with any of my desired responses, it also submits when no captcha is entered. I will continue searching for a solution.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    try this:

    PHP Code:
    if(preg_match('/^al|alabama$/i'$captcha)) {
      echo 
    'success';
    } else {
      echo 
    'Come on, really? The answer to the question is in our name!';

    you can store regular expression as string

    PHP Code:
    $regexp '/^il|illinois$/i';
    if(
    preg_match($regexp$captcha)) {
      echo 
    'success';
    } else {
      echo 
    'Come on, really? The answer to the question is in our name!';

    or array:
    PHP Code:
    $names = array('il''illinois');
    if(
    preg_match('/^' join('|'$names) . '$/i'$captcha)) { ... } 
    Last edited by poyzn; 11-07-2012 at 09:31 PM.

  • Users who have thanked poyzn for this post:

    sakina (11-07-2012)

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Finally got it working. Here is what I used:

    PHP Code:
        $error_message "";

        
    $captcha_exp = ('(Alabama|AL|Al|al|alabama|bama|Bama)');

        if( !
    preg_match($captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";


  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks for you help poyzn! I wish I would have seen that in time!

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by sakina View Post
    Finally got it working. Here is what I used:

    PHP Code:
        $error_message "";

        
    $captcha_exp = ('(Alabama|AL|Al|al|alabama|bama|Bama)');

        if( !
    preg_match($captcha_exp$captcha))
    {
        
    $error_message ="Come on, really? The answer to the question is in our name!<br />";

    hm, try this values of $captcha with your solution: california, obama, CALL etc.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •