4 Replies - 575 Views - Last Post: 26 October 2015 - 02:12 PM Rate Topic: -----

#1 El Ramo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 15

PHP Verify captcha function doesn't work, MySQL PDO error!

Posted 26 October 2015 - 02:00 PM

Hi, I'm new in this forum
I'm trying to implement a captcha code in a function inside a class, but it doesn't work:
In the code below I try to catch if there was found an id in the database's table and if it is not display an error.
But the code doesn't work! When I try to get the code from the table.
I tried to send a wrong captcha: it works, but when I send the correct captcha It display 'captcha expired'
I post below a piece of code, if you want the full class, reply me!
Thanks in advance

code:
try {
				$db=new PDO("mysql:host=$this->host;dbname=$this->database", $this->user, $this->pass, $this->options);
				$stmt=$db->prepare("SELECT `code` FROM `captcha` WHERE `id`=?");
				$stmt->execute(array($id));
				$stmt->bindColumn(1, $code);
				//captcha expired
				echo $code;
				if($stmt->fetch(PDO::FETCH_BOUND)) {
					$this->output .= "<p><b>Captcha expired!</b></p>";
					return false;
				}
				echo "code:$code, captcha:$captcha, id=$id";
				$timeout=time()-$captchatime;
				$stmt=$db->prepare("DELETE FROM `captcha` WHERE `id`=? OR `time`<?");
				$stmt->execute(array($id, $timeout));
				//wrong captcha
				if($captcha!=$code) { 
					$this->output .= "<p><b>Wrong captcha!</b></p>";
					return false;
				}


Sorry, in line 8 is
if($stmt->fetch(PDO::FETCH_BOUND)) {

but it doesn't work D:

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Verify captcha function doesn't work, MySQL PDO error!

#2 Dormilich   User is online

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: PHP Verify captcha function doesn't work, MySQL PDO error!

Posted 26 October 2015 - 02:10 PM

on line #5 you’re using the wrong method. bindColumn() is PDO’s equivalent to mysqli’s bind_result(), i.e. it binds output parameters, not input parameters (for that you’d use bindValue()).

additionally you should set PDO’s error handling to anything other than silent:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Was This Post Helpful? 0
  • +
  • -

#3 El Ramo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 15

Re: PHP Verify captcha function doesn't work, MySQL PDO error!

Posted 26 October 2015 - 02:11 PM

Sorry, I found the bug, how can I delete this topic?

Thank you Dormilich!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is online

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: PHP Verify captcha function doesn't work, MySQL PDO error!

Posted 26 October 2015 - 02:11 PM

topics are not supposed to be deleted. someone else may profit of it.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6561
  • View blog
  • Posts: 26,600
  • Joined: 12-December 12

Re: PHP Verify captcha function doesn't work, MySQL PDO error!

Posted 26 October 2015 - 02:12 PM

From the docs:

Quote

PDO::FETCH_BOUND: returns TRUE and assigns the values of the columns in your result set to the PHP variables to which they were bound with the PDOStatement::bindColumn() method

so it will always say that the captcha has expired.

You need to check something else to discover whether or not it has expired. What tells you this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1