???????? ????? (captcha) ?? PHP ????? 2

? ?????????? ?????? ??? ???????? ????? ?? PHP ? ?????? ???????? ?????? ????????????? ????????? ????? ?? ????????. ???, ???????, ???? ?????. ? ?????????????? ?????? ? ??? ?????????? ????????? ?????????? ?????:

<?php
 
  $letters = 'ABCDEFGKIJKLMNOPQRSTUVWXYZ';
 
  $caplen = 6;
  $width = 120; $height = 20;
  $font = 'comic.ttf';
  $fontsize = 14;
 
  header('Content-type: image/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 );
    $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['captcha'] = $captcha;
 
  imagepng($im);
  imagedestroy($im);
 
?>

????????? ???? ??? ? ????? ??? ?????? captcha.php ? ?????? ? ???????? ?????????? ???-???????. ????? ? ???? ?????? ?????? ???? ?????? (? ?????? ??????? ???????????? ????? comic.ttf). ?, ???????, ??????? PHP-?????? index.php, ?????????? ? ?????? ??????. ? ???? ???????? ??? ?????????:

<?php
 
  //--???? ???????? ?????
  if ( isset($_POST['captcha']) )
  {
    $code = $_POST['captcha']; //--???????? ????????? ????????????? ?????
 
    session_start();
    //--??????????
    if ( isset($_SESSION['captcha']) && strtoupper($_SESSION['captcha']) == strtoupper($code) )
      echo '?????????!';
    else
      echo '???????????!';
    //--??????? ?? ?????? ??? ?????
    unset($_SESSION['captcha']);
 
    exit();
  }
 
?>

???? HTML-??? ?????:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>???????? ????????</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  </head>
  <body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <img src="captcha.php?sid=<?php echo rand(10000, 99999); ?>" width="120" height="20" alt="" /><br />
      <input type="text" name="captcha" /><br />
      <input type="submit" value="?????????" />
    </form>
  </body> 
</html>

??????? ?????????? ? ??????. ??-??????, ???????? ???????? ?? ???????? ???????? action. ????? ????? ? ??? ??????????? ? ?????????? ???????, ?? ????????? ??????? ?????????? ????????? ?????? ????, ????????????? ??? ????? ? ?????????? ?????? ????? ???? ????????? ? ????? ?????. ???? ????? ???????? ???????????-????? ? ???? ??? ????? ??????. ??????????? ???????? ???????? ???? ? ??????? captcha.php, ? ????? ???????, ??????????? ? ?????????, ????????? ? ??????? ?????. ? ???????? ??????????? ? ??????? GET-???????? sid ?? ????????? ???????? ????????? ??? ????, ????? ?????? ??? ???? ? ???????? ??? ?????? ? ??????? ?? ????????? ?????.

? ??????????? ??????? ?????????? ????????? ????????????? ?????:

$code = $_POST['captcha'];

? ????? ??????? ?????????? ????? ?????? ????? ?????? ??? ?????????? ? ?????????? ??????????. ??????? ????????? ???????? ????????? ???????:

if ( isset($_SESSION['captcha']) && strtoupper($_SESSION['captcha']) == strtoupper($code) )

??-??????, ??????????? ??????? ??????????? ?????? ? ?????????? ??????????. ?????????? ????? ?????????????, ???? ? ???????? ??????? ????????? ???????? ? ?? ?? ???????? ?????. ??-??????, ????? ???????????? ???????????? ? ?????????? ???????. ??? ?????? ?????????????? ?????????? ? ??????? ??????? ??? ????, ????? ??????? ???????? ?? ?????????? ??? ?????????.

????? ????????? ?????????? ??? ????? ????????? ?? ??????. ??? ????? ?????? ???????????, ????? ????? ?????????? ????????? ????????: ????????????? (??????) ???? ??? ??????????? ?????, ?????????? ?? ???. ????? ? ??????? ????? ???? ?? ????? ??????? ?????? ??? ?????? ????????, ???? ?? ???????? ????? ?????.

??????? ????? ???????.

Комментарии

Оставить комментарий »

 
Joker-jar
28 ноября 2012, 9:28
#21
 

??????, ? error-???? ???-???????.

?????????
7 декабря 2012, 0:00
#22
 

?????? ????? ?????

<form action="<?php echo JRoute::_( 'index.php' ); ?>" method="post" id="josForm2" name="josForm2" class="form-validate">
<input type="hidden" name="popup" value="" />
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="contentpane">
 
	<tr>
		<td width="30%" height="30">
		<label id="namemsg" for="name">*<?php echo JText::_( 'Name' ); ?>: </label>
		<input type="text" name="name" id="name" value="<?php echo $user->name;?>" required="required" class="inputbox required" maxlength="50" /></td>
	</tr>
 
 
	<tr>
		<td height="30">
		<label id="emailmsg" for="email">* <?php echo JText::_( 'Email' ); ?>: </label>
		<input type="text" id="email" name="email" value="<?php echo $user->email;?>" required="required" class="inputbox required validate-email" maxlength="100" /></td>
	</tr>
 
 
	<tr>
		<td width="30%" height="30">
		<label id="namemsg" for="phone">* <?php echo JText::_( 'Phone' ); ?>: </label>
		<input type="text" name="phone" id="phone" value="<?php //echo $this->user->get( 'name' );?>" required="required" class="inputbox required" maxlength="50" /></td>
	</tr>
 
 
	<tr>
		<td height="30">
		<label id="textmsg" for="text" >*
			<?php echo JText::_( 'Message' ); ?>:
		</label>
 
		<textarea name="text" id="text" rows="3"></textarea</td>
	</tr>   
 
 
	<tr>
		<td width="30%" height="30">
		<img src="captcha.php?sid=<?php echo rand(10000, 99999); ?>" width="120" height="30" alt="" /><br />
		<input name=\"captcha\" size=5 type=\"text\" required="required"/>
		<input type="submit" value="????????? ? ?????????" /></td>
	</tr>
</table>
</form>

?????? ??? ? index.php

<?php
 
  //--???? ???????? ?????
  if ( isset($_POST['captcha']) )
  {
    $code = $_POST['captcha']; //--???????? ????????? ????????????? ?????
 
    session_start();
    //--??????????
    if ( isset($_SESSION['captcha']) && strtoupper($_SESSION['captcha']) == strtoupper($code) )
      echo '?????????!';
    else
      echo '???????????!';
    //--??????? ?? ?????? ??? ?????
    unset($_SESSION['captcha']);
 
    exit();
  }
 
?>

??? ? captcha.php

<?php
 
  $letters = 'ABCDEFGKIJKLMNOPQRSTUVWXYZ';
 
  $caplen = 6;
  $width = 120; $height = 20;
  $font = 'comic.ttf';
  $fontsize = 14;
 
  header('Content-type: image/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 );
    $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['captcha'] = $captcha;
 
  imagepng($im);
  imagedestroy($im);
 
?>

???????? ????????????, ?? ?? ??????? ?? ?????? "?????????" ?? ???????????... ??????????, ??? ? ??? ? ?????...

?????????
7 декабря 2012, 0:05
#23
 

? ??? ? ?????, ??? ???? ??? ????? ? ?????? ??????, ?? ??? ??? ?? ????????, ??????????, ??? ??????? ????? ??? index-a, ????? ???????? ????????...?

Joker-jar
7 декабря 2012, 22:31
#24
 

??? ???? ??? ????? ? ?????? ??????, ?? ??? ??? ?? ????????, ??????????, ??? ??????? ????? ??? index-a, ????? ???????? ????????...?

????? ????? ?????????? ?? ? ????? ?????, ?? ??????????? ? index.

?????????
10 декабря 2012, 22:24
#25
 

?????? ????? ?????? ?????????! ? ???? ???? ??????? ????? ???????? ?????, ?? ? ??? ??? ??????, ? ??????? ? ??? ????? ???? ?????? ? ???? ????? ??????. ???????? ????????????, ? ???? ??????????? ??? ??????????. ??????: ??? ??? ????????? ???????? ?????? ? <button class="button validate" type="submit"><?php echo JText::_('Send'); ?></button> ?

Joker-jar
10 декабря 2012, 23:19
#26
 

?????????, ??? ?????? ????? ??????????, ? ????? ??????? ?????????? ????????? ???????? ??????. ????????? ? ???????? ??? ???????? ? ??????, ??????? ??? ????? form ? ?????????? ?? ???????? ???????? action. ?????????? ???? ???? ? ?????????, ???????? ?????????? ???????? ?????? ?????. ??? ???? ? ????? ???????? ??? ???????? ?????, ?? ??? ??? ??????? ??? ??????? ?? ????.

?????????
11 декабря 2012, 0:31
#27
 

<form action="<?php echo JRoute::_( 'index.php' ); ?>" ... ??? index.php ???????? ?????????? ??? ???, ? ??????? ????????? ???? ? ???????!

Joker-jar
11 декабря 2012, 0:42
#28
 

?????????, ???????? ???????? ??? ?????????????? ???????? ????? ? ????????, ??? ????? ????????.

?????????
11 декабря 2012, 1:03
#29
 

???????! ???? ??????????! :-)

??????
10 марта 2013, 8:38
#30
 

??????? ??????? ?? ??????, ???????? ?????????? ??????. ?????? ??? ??? ? ????? ?????? ?? ???????, ?? ??? ????? ???????
+100500 ??????.

Оставить комментарий

Ваше имя
 
Ваш e-mail
 
Комментарий