How can I organize a display of captcha before the first entrance to the site? (code please)
Closed due to the fact that the essence of the question is not clear by the participants aleksandr barakin , 0xdb , freim , Alex , andreymal 4 May at 16:25 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Alternatively, you can make a captcha validation and write the result of processing in cookie / session, and redirect to authorization where this result will be checked. But for the first time I see captcha doing this .. - MoloF
- It is necessary in practice, in theory, it is clear - Roman Grishin
- oneWell, maybe you can open up the question in more detail and put at least the code of what you have? It helps to solve the problem / question, but not to do everything from scratch .. - MoloF
- What kind of code? There is a simple default site with several pages in PHP. Everything. Code pages does not matter. - Roman Grishin
- fourPossible duplicate question: Show captcha before entering - u_mulder
|
1 answer
Google before writing here http://lifeexample.ru/php-primeryi-skriptov/kapcha-na-php.html
And here is an example from the site:
ΠΡΠΆΠ½ΠΎ Π·Π°ΠΏΠΈΠ»ΠΈΡΡ 5 ΡΠ°ΠΉΠ»ΠΎΠ²: index.php β ΡΠΊΡΠΈΠΏΡ Π²ΡΠ²ΠΎΠ΄ΡΡΠΈΠΉ Π½Π°ΡΡ ΠΊΠ°ΠΏΡΡ Π½Π° PHP; validator.php β ΡΠΊΡΠΈΠΏΡ ΠΏΡΠΎΠ²Π΅ΡΡΡΡΠΈΠΉ ΡΡ
ΠΎΠ΄ΡΡΠ²ΠΎ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΎΠΉ; capcha.php β ΡΠΊΡΠΈΠΏΡ Π³Π΅Π½Π΅ΡΠΈΡΡΡΡΠΈΠΉ ΠΊΠ°ΡΡΠΈΠ½ΠΊΡ; comic.ttf β ΡΡΠΈΡΡ ΡΠ΅ΠΊΡΡΠ°; bg_capcha.png β ΡΠΎΠ½ ΠΏΠΎΠ΄ ΡΠ΅ΠΊΡΡΠΎΠΌ.
index.php
ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΊΠΎΠ΄ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ: <br/> <img style="border: 1px solid gray; background: url('bg_capcha.png');" src = "captcha.php" width="120" height="40"/> <br/> <form action="validator.php" method="POST"> <input type="text" name="capcha" /> <input type="submit" name="send" value="ΠΡΠΎΠ²Π΅ΡΠΈΡΡ"/> </form>
capcha.php
$letters = 'ABCDEFGKIJKLMNOPQRSTUVWXYZ'; // Π°Π»ΡΠ°Π²ΠΈΡ $caplen = 6; //Π΄Π»ΠΈΠ½Π° ΡΠ΅ΠΊΡΡΠ° $width = 120; $height = 40; //ΡΠΈΡΠΈΠ½Π° ΠΈ Π²ΡΡΠΎΡΠ° ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ $font = 'comic.ttf';//ΡΡΠΈΡΡ ΡΠ΅ΠΊΡΡΠ° $fontsize = 14;// ΡΠ°Π·ΠΌΠ΅Ρ ΡΠ΅ΠΊΡΡΠ° header('Content-type: image/png'); //ΡΠΈΠΏ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ³ΠΎ (ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ° Π² ΡΠΎΡΠΌΠ°ΡΠ΅ PNG) $im = imagecreatetruecolor($width, $height); //ΡΠΎΠ·Π΄Π°ΡΡ Π½ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ imagesavealpha($im, true); //ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΠΏΡΠΎΠ·ΡΠ°ΡΠ½ΠΎΡΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ $bg = imagecolorallocatealpha($im, 0, 0, 0, 127); //ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠ²Π΅ΡΠ° Π΄Π»Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ imagefill($im, 0, 0, $bg); //Π²ΡΠΏΠΎΠ»Π½ΡΠ΅Ρ Π·Π°Π»ΠΈΠ²ΠΊΡ ΡΠ²Π΅ΡΠΎΠΌ putenv( 'GDFONTPATH=' . realpath('.') ); //ΠΏΡΠΎΠ²Π΅ΡΡΠ΅Ρ ΠΏΡΡΡ Π΄ΠΎ ΡΠ°ΠΉΠ»Π° ΡΠΎ ΡΡΠΈΡΡΠ°ΠΌΠΈ $captcha = '';//ΠΎΠ±Π½ΡΠ»ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡ for ($i = 0; $i < $caplen; $i++) { $captcha .= $letters[ rand(0, strlen($letters)-1) ]; // Π΄ΠΎΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΡΠ»ΡΡΠ°ΠΉΠ½ΡΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΠΈΠ· Π°Π»ΡΠ°Π²ΠΈΠ»Π° $x = ($width - 20) / $caplen * $i + 10;//ΡΠ°ΡΡΠΎΡΠ½ΠΈΠ΅ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠΈΠΌΠ²ΠΎΠ»Π°ΠΌΠΈ $x = rand($x, $x+4);//ΡΠ»ΡΡΠ°ΠΉΠ½ΠΎΠ΅ ΡΠΌΠ΅ΡΠ΅Π½ΠΈΠ΅ $y = $height - ( ($height - $fontsize) / 2 ); // ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Y $curcolor = imagecolorallocate( $im, rand(0, 100), rand(0, 100), rand(0, 100) );//ΡΠ²Π΅Ρ Π΄Π»Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ Π±ΡΠΊΠ²Ρ $angle = rand(-25, 25);//ΡΠ»ΡΡΠ°ΠΉΠ½ΡΠΉ ΡΠ³ΠΎΠ» Π½Π°ΠΊΠ»ΠΎΠ½Π° imagettftext($im, $fontsize, $angle, $x, $y, $curcolor, $font, $captcha[$i]); //Π²ΡΠ²ΠΎΠ΄ ΡΠ΅ΠΊΡΡΠ° } // ΠΎΡΠΊΡΡΠ²Π°Π΅ΠΌ ΡΠ΅ΡΡΠΈΡ Π΄Π»Ρ ΡΠΎΡ
ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠ³Π΅Π½Π΅ΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅ΠΊΡΡΠ° session_start(); $_SESSION['capcha'] = $captcha; imagepng($im); //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ imagedestroy($im);//ΠΎΡΡΠΈΡΠ°Π΅ΠΌ ΠΏΠ°ΠΌΡΡΡ
validator.php
session_start(); if($_POST['capcha'] != $_SESSION['capcha']) echo "Π’Π΅ΠΊΡΡ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ Π²Π²Π΅Π΄Π΅Π½ Π½Π΅ Π²Π΅ΡΠ½ΠΎ!"; else echo "Π£ΡΠ° ΡΠ΅ΠΊΡΡ ΡΠΎΠ²ΠΏΠ°Π»!";
|