...

View Full Version : Stupid Question- how to assign more than 1 result to a variable



sakina
11-07-2012, 08:13 AM
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:


$captcha_exp = '/Alabama/';

tangoforce
11-07-2012, 01:54 PM
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.

sakina
11-07-2012, 09:23 PM
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.

sakina
11-07-2012, 09:24 PM
My code if it helps any future readers:


$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 />";
}

sakina
11-07-2012, 09:55 PM
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.

poyzn
11-07-2012, 10:22 PM
try this:


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



$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:


$names = array('il', 'illinois');
if(preg_match('/^' . join('|', $names) . '$/i', $captcha)) { ... }

sakina
11-07-2012, 10:36 PM
Finally got it working. Here is what I used:


$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 />";
}

sakina
11-07-2012, 10:36 PM
Thanks for you help poyzn! I wish I would have seen that in time!

poyzn
11-07-2012, 10:42 PM
Finally got it working. Here is what I used:


$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. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum