Skip to content

Commit 34d127c

Browse files
authored
adding capatcha class and demo file
1 parent dd7c1ec commit 34d127c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

captcha/captcha.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
session_start();
3+
/**
4+
* PHP CAPTCHA CLASS
5+
* WRITTEB BY : SEMICOLON
6+
* AHMED MAHER HALIMA
7+
* https://www.facebook.com/ahmedmaherhalima
8+
**/
9+
/**
10+
*
11+
*/
12+
class Captcha
13+
{
14+
public $img;
15+
public $txtCol;
16+
public $fontSize = 40;
17+
public $imageWidth = 80;
18+
public $imageHeight = 20;
19+
public $string;
20+
21+
public function createImg(){
22+
$this->img = imagecreate($this->imageWidth, $this->imageHeight);
23+
imagecolorallocate($this->img, 255, 255, 255);
24+
$this->txtCol = imagecolorallocate($this->img, 0, 0, 0);
25+
//draw lines on image
26+
for($i = 0; $i <= 30; $i++ ){
27+
$x1 = rand(1,100);
28+
$y1 = rand(1,100);
29+
$x2 = rand(1,100);
30+
$y2 = rand(1,100);
31+
imageline($this->img, $x1, $y1, $x2, $y2,$this->txtCol);
32+
}
33+
// add captcha string to image
34+
$this->string = md5(microtime());
35+
$this->string = substr($this->string, 0 , 6);
36+
$_SESSION['secret'] = $this->string;
37+
imagestring($this->img, $this->fontSize, 0, 0, $this->string, $this->txtCol);
38+
imagepng($this->img);
39+
} // end createImg function
40+
41+
}
42+
43+
$captcha = new Captcha();
44+
$captcha->createImg();
45+
?>

captcha/index.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
//usage of the class
3+
4+
//confirmation
5+
session_start();
6+
if (isset($_POST['check'])) {
7+
if(!empty($_POST['input']) && $_POST['input'] == $_SESSION['secret']){
8+
echo 'good';
9+
}else{
10+
echo 'wrong';
11+
}
12+
}
13+
?>
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<title>captcha</title>
18+
</head>
19+
<body>
20+
<img src="captcha.php" /> <br />
21+
enter the captcha :
22+
<form method="post">
23+
<input type="text" name="input">
24+
<input type="submit" name="check" value="submit" />
25+
</form>
26+
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)