HTML_QuickForm and Text_CAPTCHA creating 2 files

From: Date: Wed, 21 Mar 2007 14:31:26 +0000
Subject: HTML_QuickForm and Text_CAPTCHA creating 2 files
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-26891@lists.php.net to get a copy of this message
I have just come up with a "work around" solution to a problem that has been a thorn in my side. I thought I would share this problem/solution with the pear list in the hopes that it will help someone else or spur some better information than I am providing. DISCLAIMER: I am new to using Pear. So, my problem probably could have been avoided by using HTML_QuickForm_CAPTCHA. But, for various reasons (like I would have had to redo the whole page and I didn't understand some of how it worked), I gave it a quick try, but I did not use it. The setup for the problem is this: I have a registration form that uses HTML_Quickform for the registration part and uses Text_CAPTCHA to create the image. The problem I was having was that it was creating two images and only deleting one. So, I was getting a buildup of superfluous files in my tmp directory. It didn't strike me as being odd that it was creating two files right away because one of my unlink's was working. So, I couldn't tell it was creating two. Then, upon watching the directory very carefully, I noticed it. But, because of how it was deleting one and creating another, it looked like the filename was changing. This was the tricky part. Once I figured that out, I put unlink($filename) statements everywhere. It would still only delete one of the files. Work around: I never really figured out why it was creating the second captcha image. I've gone over my code with a fine tooth come. And, I just can't see why my form would be called twice. But, to get around it, I deleted it from the page I refer to after successful registration by looking at the session variable where I store the captcha information. I hope this helps someone. Here's the register.php file (keep in mind that the last unlink occurs outside of this file in the file that the header(Location: xxxx) points to). Hopefully, the formatting stays somewhat intact: <?php session_start(); require_once('../libs/NewInit.php'); require_once 'HTML/QuickForm.php'; require_once 'Text/CAPTCHA.php'; require_once 'Text/Password.php'; require_once 'Mail.php'; require_once 'Mail/mime.php'; $form = new HTML_QuickForm('registerform', 'post', $_SERVER['REQUEST_URI']); $CAPTCHA_FILE_TYPE = '.png'; $db = new xSQL(); if ($form->isSubmitted()) { if ($form->validate()) { $data = $form->getSubmitValues(); $submittedName = mysql_real_escape_string($data['name']); $sql = "select * from users where username = '$submittedName'"; $db->query($sql, SQL_ALL, SQL_ASSOC); $alreadyUsedName = $db->record[0]['username']; $file = 'captcha_tmp/' . md5($_SESSION['secretCAPTCHA']) . $CAPTCHA_FILE_TYPE; if (empty($alreadyUsedName)) { /* means name is available */ if (isset($_POST['CAPTCHA']) && isset($_SESSION['secretCAPTCHA']) && strlen($_POST['CAPTCHA']) > 0 && strlen($_SESSION['secretCAPTCHA']) > 0 && $_POST['CAPTCHA'] == $_SESSION['secretCAPTCHA']) { if (file_exists($file)) { unlink($file); } $password = Text_Password::create(); // strip white space (no multiword usernames allowed) $data['name'] = preg_replace('/\s+/', '', $data['name']); $sql = "INSERT INTO users (username, password) VALUES ('" . $db->escapeSimple($data['name']) . "', '" . md5($password) . "')"; $db->query($sql); $username = htmlspecialchars($db->escapeSimple(strip_tags($data['name']))); $prefmanager->setPref($username, 'name', $db->escapeSimple($data['name'])); $prefmanager->setPref($username, 'level', '1'); $text = <<<EOD Welcome to the forums {$data['name']}. You can log in using the username {$data['email']} and the password $password. EOD; $html = '<html><body><p>Welcome to the forums ' . $data['name'] . '</p><p>You can log in using the username "' . $data['name'] . '" and the password ' . $password . 'The filename is: ' . $file . '<br/>$_SESION[secretCAPT] is: ' . $_SESSION['secretCAPTCHA'] . '</p></body></html>'; $crlf = "\n"; $hdrs = array( 'From' => 'me@mydomain.com', 'To' => $data['email'], 'Subject' => 'Your new forum username and password' ); $hostInfo['host'] = 'mail.domain.com'; $mime = new Mail_mime($crlf); $mime->setTXTBody($text); $mime->setHTMLBody($html); $body = $mime->get(); $hdrs = $mime->headers($hdrs); $mail =& Mail::factory('smtp', $hostInfo); $mail->send($data['email'], $hdrs, $body); header('Location: otherpage.php?new=true'); } else { if (file_exists($file)) unlink($file); $message = "Oops, the code you entered did not match the one in the image. Please try again"; } } else { if (file_exists($file)) {unlink($file);} $message = "Sorry. The username: \"$alreadyUsedName\" has been taken. Please try another again."; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>New Member Registration</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <?php $options = array( 'font_size' => 32, 'font_path' => './', 'font_file' => 'DejaVuSerif-Bold.ttf' ); if (isset($_SESSION['secretCAPTCHA']) && file_exists("captcha_tmp/" . md5($_SESSION['secretCAPTCHA']) . ".png")) unlink("captcha_tmp/" . md5($_SESSION['secretCAPTCHA']) . ".png"); $formCAPTCHA = Text_CAPTCHA::factory('Image'); $result = $formCAPTCHA->init(300, 120, null, $options); $_SESSION['secretCAPTCHA'] = $formCAPTCHA->getPhrase(); $imageCAPTCHA = $formCAPTCHA->getCAPTCHAAsPNG(); $phrase = $formCAPTCHA->getPhrase(); $filename = 'captcha_tmp/' . md5($phrase) . $CAPTCHA_FILE_TYPE; // $logFileHandle = fopen("captcha_tmp/log.txt", 'a'); // fwrite($logFileHandle, $filename . "\n"); // fclose($logFileHandle); $form->addElement('text','name', 'Username (5-15 Characters):'); $form->addElement('text', 'email', 'Email Address:'); $form->addElement('text', 'CAPTCHA', 'Code displayed in above image:'); $form->addElement('submit', null, 'Submit', 'class=inputButton'); $form->applyFilter('name', 'trim'); $form->applyFilter('email', 'trim'); $form->addRule('name', 'Please provide your name', 'required', null, 'client'); $form->addRule('name', 'The username must be between 5 and 15 characters long', 'rangelength', array(5,15), 'client'); $form->addRule('email', 'Please provide an email address', 'required', null, 'client'); $form->addRule('email', 'Please enter a valid email address', 'email', null, 'client'); $form->addRule('CAPTCHA','Please enter the code in the image', 'required', null, 'client'); if (isset($_SESSION['secretCAPTCHA']) && file_exists("captcha_tmp/" . md5($_SESSION['secretCAPTCHA']) . ".png")) unlink("captcha_tmp/" . md5($_SESSION['secretCAPTCHA']) . ".png"); ?> <div class='topSection'> <h1 class="title">New Member Registration</h1> </div> <div class='bodySection'> <?php file_put_contents($filename, $imageCAPTCHA); echo "<img style='display: block; margin: auto auto' src='$filename' alt='captcha'><br/>"; if (isset($message)) echo "<tr><td colspan='2'><p style=\"color: red; font-size: 20px;\"><strong>" . $message . "</strong></p></td></tr>"; $form->display(); ?> <p style='text-align: center; padding-top: 30px; margin: 0 auto'><a href="member.php">Return to login page</a></p> </div> </body> </html> ____________________________________________________________________________________ Be a PS3 game guru. Get your game face on with the latest PS3 news and previews at Yahoo! Games. http://videogames.yahoo.com/platform?platform=120121

« previous php.pear.general (#26891) next »