Back to Home

Cana Captcha in PHP - It's Easy!

captcha · captcha · capcha · captcha · I have a brain captcha

Cana Captcha in PHP - It's Easy!


    In this topic, I will briefly talk about Kana-CAPTCHA, what it is, how it works and how to make it using PHP.



    Katakana


    In Japanese, two syllable alphabets (katakana, hiragana) and pro-Chinese characters (kanji) are used for recording. By the way, kanji have little in common with the pronunciation of Chinese characters. They were brought to Japan at the beginning of our era and they developed in their own way (manegan). There are countless kanjeys (the knowledge of 2-4 thousand kanji is considered to be the highest level of Japanese education). In the case of syllabic alphabets, everything is simpler, hiragana (can be used to write anything) has 47 basic characters, the same number of characters is in the katakana (used mainly for writing borrowed words, for example モ ニ タ (mo-ni-ta) - monitor).

    Syllabic alphabets are so named precisely because each symbol is a syllable consisting of one consonant and one vowel (with the exception of vowels and the consonant "n", these signs have their own symbols). The main one is hiragana (in some cases, transcription of hiragana is prescribed over kanji, for the convenience of poorly educated people), while the katakana is secondary. It is her that we will consider today.

    And we will consider it because the Katakana characters are the easiest to write, therefore, we, 馬鹿 外人, will be easier to enter and recognize them. By the way, Japanese children study Katakana first of all, for the same reason.

    A small addition - in the picture at the beginning of the topic katakana it says "チ ュ ー リ ン グ テ ス ト" (ti-u-ri-n-gu-te-su-that), which means "Turing test". Turing is written in katakana, as it is a foreign proper name. In the case of “Test”, the reason is that this word is borrowed.

    Development


    In the meantime, remember what for us, 馬鹿 外人, kana-captcha. This is an image with the symbols of katakana \ hiragana, in the field you need to enter transcription . Just because not everyone 外人 in the system has support for entering the characters we need.

    As a basis, I will take my script , and only slightly modify it. To begin with, we will determine what our kana-captcha will be like. It will be two or three black Katakana symbols on a white background + intersecting black lines.

    For starters, I want to get rid of all the obscenities in my script. I remove the vyrviglazny background, I make the text and lines black. We get this:


    Now it's time to shove the katakana into our picture. First of all, let's decide on the font, for Japanese syllable alphabet, I consider the best version of MS Gothic , the cutest font. To my surprise, the .TTC font was accepted by PHP absolutely normal. Honestly, I expected a lot of hemorrhoids with this type of TrueType font.

    Next, you need to modify the script that generates the captcha code. Firstly, to feed him new characters, namely: "アイウエオカキクケコサシスセソタチツテトナニネヌノハヒフヘホマミムメモヤユヨラリルレロワン". These are all the main characters of the Katakana. The generation function will look like this:
    	function generate_code() 
    	{    
    		  $chars = 'アイウエオカキクケコサシスセソタチツテトナニネヌノハヒフヘホマミムメモヤユヨラリルレロワン'; 
    		  $length = rand(2, 3); 
    		  $numChars = mb_strlen($chars, "UTF-8"); 
    		  $str = '';
    		  for ($i = 0; $i < $length; $i++) {
    			$str .= mb_substr($chars, rand(1, $numChars) - 1, 1, "UTF-8");
    		  } 
    		return $str;
    	}
    Note! I use multibyte variants of strlen and substr functions.

    Then, a little bit we’ll draw up the random positions of the characters in the generation script, we will also introduce multibyte functions:
    		$x = rand(0, 35);
    		for($i = 0; $i < mb_strlen($code, "UTF-8"); $i++) {
    			$x+=27;
    			$letter=mb_substr($code, $i, 1, "UTF-8");
    			imagettftext ($im, $font_arr[$n]["size"], rand(3, 4), $x, rand(54, 55), "000000", img_dir.$font_arr[$n]["fname"], $letter);
    		}
    As a result, we get this:


    Almost done. I checked the operation of the captcha by entering the correct version exactly with the katakana symbols. But what do 馬鹿 外人 do not have support for entering Japanese characters? It is necessary to make sure that the validation takes place with transcription. To do this, you have to write an entire function:
    function kanatoroma($str){
    		$replace_of = array('ア','イ','ウ','エ','オ','カ','キ','ク','ケ','コ',
    			'サ','シ','ス','セ','ソ','タ','チ','ツ','テ','ト','ナ','ニ','ネ','ヌ',
    			'ノ','ハ','ヒ','フ','ヘ','ホ','マ','ミ','ム','メ','モ','ヤ','ユ','ヨ',
    			'ラ','リ','ル','レ','ロ','ワ','ン');
    		$replace_by = array('a','i','u','e','o','ka','ki','ku',
    			'ke','ko','sa','shi','su','se','so','ta','chi','tsu','te',
    			'to','na','ni','ne','nu','no','ha','hi','fu','he','ho','ma',
    			'mi','mu','me','mo','ya','yu','yo','ra','ri','ru','re','ro','wa','n');
            $_result = str_replace($replace_of, $replace_by, $str);
            return $_result;
        }

    With the help of this wonderful function, we save the correct captcha solution in the form of romaji into the session.
    That's all :) Kana-captcha is ready!
    To test myself for the knowledge of katakana
    Cheat

    By the way, I took this domain on purpose, so that in the future I would host all sorts of nishty under it that I will publish on the hub :)

    And yes, this is not my last topic about captcha, the most interesting is ahead :)

    Read Next