Pregunta

Is it possible to use the "die" tag to link to an error site, instead of the text that is displayed by default?

require_once('recaptchalib.php');
$privatekey = "*******";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}

Like:

die ('error.php');

Of course this doesn't work. But I can't figure out how to use the die tag.

¿Fue útil?

Solución

die() cannot do anything other than print the passed value and stop the script, however your code already has the structure to do what you want:

if (!$resp->is_valid) {
    header('Location: http://example.com');
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
        "(reCAPTCHA said: " . $resp->error . ")");
}

Note that if you've output anything before this code, you'll have to use a different method to redirect as header() will not work after headers have been sent (by starting output).

It's worth noting that you would probably be better off changing the flow of the program if the captcha didn't match rather than redirecting and stopping execution. If you redirect (as is) you lose all data associated with the current request, whereas if you set something like $captcha_error = true and continue the program (changing flow where appropriate based on $captcha_error), you end up with a better user experience since you're not resetting an entire form because the captcha was wrong.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top