diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2788797 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "kamal/math-captcha", + "description": "Math captcha in PHP", + "license": "MIT", + "authors": [ + { + "name": "Kamal Pandey", + "email": "kml.pandey77@gmail.com" + } + ], + "autoload": { + "psr-4": { + "Captcha\\": "src/Captcha" + } + }, + "require": {} +} diff --git a/src/Captcha/Captcha.php b/src/Captcha/Captcha.php new file mode 100644 index 0000000..595355c --- /dev/null +++ b/src/Captcha/Captcha.php @@ -0,0 +1,73 @@ +first_num = rand(1, 10); + $this->second_num = rand(1, 10); + $operator = rand(0, count($this->operators) -1); + $this->operator = $this->operators[$operator]; + + if($this->operator == '-' && $this->first_num < $this->second_num){ + $first = $this->second_num; + $second = $this->first_num; + $this->second_num = $second; + $this->first_num = $first; + } + + $this->calculater(); + } + + protected function calculater() + { + switch($this->operator){ + case '*': + $this->answer = $this->first_num * $this->second_num; + break; + + case '+': + $this->answer = $this->first_num + $this->second_num; + break; + + case '-': + $this->answer = $this->first_num - $this->second_num; + break; + + } + + $_SESSION['anser'] = $this->answer; + } + + public function check() + { + return isset($_POST['captcha']) && isset($_SESSION['anser']) ? + ((int) $_POST['captcha'] == $_SESSION['anser']) + :false; + } + + public function math() + { + return "{$this->first_num} {$this->operator} {$this->second_num}"; + } + + function __toString() + { + return $this->math(); + } + + + +} + + diff --git a/test/index.php b/test/index.php new file mode 100644 index 0000000..313cfd6 --- /dev/null +++ b/test/index.php @@ -0,0 +1,27 @@ + + +
+

Answer it
+ +

+

+
+