use mybb's captcha on my custom page
#1
Hi everybody!
Not a programmer but I have created a new page called "NEWS" which everybody can submit their posts. I'm using some class and function exist in MyBB. Many thank to mybb team for their power, clean and easy to use code.

I'm having a trouble. Captcha's image was showed on my "news posting page". But i don't know how to verify it when someone click Submit button.

I has this code to show captcha:
require_once MYBB_ROOT.'inc/class_captcha.php';
if($mybb->settings['captchaimage'] == 1){
$captcha = new captcha(true, "post_captcha");
$captcha = $captcha->html;	
}

Then i add it to my template:
<div>{$captcha}</div>

then i tried to process a form
if (isset($_POST['news_post'])) {
require_once MYBB_ROOT.'inc/class_captcha.php';
		$captcha = new captcha;
			if($captcha->validate_captcha() == false)
			{
				// CAPTCHA validation failed, handle the error
					$errors[] = $lang->error_regimageinvalid;
			}
}

( i copied the code above from mybbstyle then edit something because the original code not work for me. It's here)
Then, i still post a news without captcha.
I'm really an amateur and hope to be teach by someone.
Thank in advance!
Reply
#2
This user has been denied support. This user has been denied support.
showing the captcha

if($mybb->settings['captchaimage'] == 1 && function_exists('imagepng') && !$mybb->user['uid'])
{
        $randomstr = random_str(5);
        $imagehash = md5(random_str(12));
        $imagearray= array(
                'imagehash' => $imagehash,
                'imagestring' => $randomstr,
                'dateline' => TIME_NOW
        );
        $db->insert_query('captcha', $imagearray);
        eval("\$captcha = \"" . $templates->get('post_captcha') . "\";");
}

verifying the captcha

        if($mybb->settings['captchaimage'] == 1 && function_exists("imagepng") && !$mybb->user['uid'])
        {
                $imagehash = $db->escape_string($mybb->input['imagehash']);
                $query = $db->simple_select('captcha', '*', 'imagehash="' . $imagehash . '"');
                $imgcheck = $db->fetch_array($query);
                if(my_strtolower($imgcheck['imagestring']) != my_strtolower($imagestring) || !$imgcheck['imagehash'])
                {
                        $errors[] = $lang->error_regimageinvalid;
                }
                $db->delete_query('captcha', 'imagehash="' . $imagehash . '"');
        }
Reply
#3
May i delete
&& !$mybb->user['uid']
because i want everyone have to pass the captcha.

I have to replace something in your code

1: i add this row:
$imagestring = $mybb->input['imagestring']; //With imagestring is the name of input element for captcha on the form

if($mybb->settings['captchaimage'] == 1 && function_exists("imagepng"))
        {
                $imagestring = $mybb->input['imagestring']; //With imagestring is the name of input element...
		$imagehash = $db->escape_string($mybb->input['imagehash']);
                $query = $db->simple_select('captcha', '*', 'imagehash="' . $imagehash . '"');
                $imgcheck = $db->fetch_array($query);
                if(my_strtolower($imgcheck['imagestring']) != my_strtolower($imagestring) || !$imgcheck['imagehash'])
                {
                        error($lang->error_invalid_captcha);
				}
                $db->delete_query('captcha', 'imagehash="' . $imagehash . '"');
				
        }

2: i replace
$errors[] = $lang->error_regimageinvalid;
with:
error($lang->error_invalid_captcha); // error_invalid_captcha is a variable defined on news.lang.php

I don't know why but this function work fine now. Thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)