Re: captcha image not unlinking
| From: | Micah Gersten | Date: | Fri, 12 Sep 2008 21:28:48 +0000 |
| Subject: | Re: captcha image not unlinking | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-29256@lists.php.net to get a copy of this message | ||
I would suggest running a cron to remove the captchas after about 10
minutes. Sessions might time out and people might not postback.
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
Darren Rook wrote:
> Hi
> Please could you look at this script and offer some advice. The problem i'm having is the
> png image created through Text_captcha isn't removed off my server each time and they are
> building up.
>
> Here's my script;
>
> #######/*captcha - test if captcha is set*/##############
>
> if (!function_exists('file_put_contents')) {
> function file_put_contents($filename, $content) {
> if (!($file = fopen($filename, 'w'))) {
> return false;
> }
> $n = fwrite($file, $content);
> fclose($file);
> return $n ? $n : false;
> }
> }
>
> // Start PHP session support
> session_start();
> $captchaOk = false;
>
> if ($_SERVER['REQUEST_METHOD'] == 'POST') {
>
> if (isset($_POST['captcha_phrase']) &&
> isset($_SESSION['captcha_phrase']) && strlen($_POST['captcha_phrase'])
> > 0 && strlen($_SESSION['captcha_phrase']) > 0 &&
> $_POST['captcha_phrase'] == $_SESSION['captcha_phrase'] &&
> strlen($_POST['name'] && $_POST['email'] &&
> $_POST['emailMessage']) > 0) {
> $captchaMessage = 'Captcha Accepted!';
> $captchaOk = true;
> unset($_SESSION['captcha_phrase']);
>
> } else {
>
> $captchaMessage = 'Captcha incorrect or info not filled in!';
>
> }
> @unlink(md5(session_id()) . '.png');
> }
>
>
> #####################################################################
>
> if (!$captchaOk) {
>
> require_once 'Text/CAPTCHA.php';
>
> // Set CAPTCHA image options (font must exist!)
> $imageOptions = array('font_size'=> 24, 'font_path'=>
> 'fonts', 'font_file'=>'MINIONWB.TTF',
> 'text_color'=>'#FFFFFF', 'lines_color'=>'#FFFFFF',
> 'background_color'=>'#000000');
>
> // Set CAPTCHA options
> $options = array('width' => 150, 'height' => 80,
> 'output' => 'png', 'imageOptions' => $imageOptions);
>
> // Generate a new Text_CAPTCHA object, Image driver
> $c = Text_CAPTCHA::factory('Image');
> $retval = $c->init($options);
> if (PEAR::isError($retval)) {
> echo 'Error initializing CAPTCHA!';
> exit;
> }
>
> // Get CAPTCHA secret passphrase
> $_SESSION['captcha_phrase'] = $c->getPhrase();
>
> // Get CAPTCHA image (as PNG)
> $png = $c->getCAPTCHA();
> if (PEAR::isError($png)) {
> echo 'Error generating CAPTCHA (maybe font missing?)!';
> exit;
> }
> file_put_contents(md5(session_id()) . '.png', $png);
> }
> ##############################################################
>
>
> if (isset($_POST['submit'])) { //handle Form
>
>
> //check Name
> if(empty($_POST['name'])) {
> $name = FALSE;
> $messageProblem .= 'Please enter your name<br />';
> } else {
> $name = stripslashes(trim($_POST['name']));
> $messageOK .= 'name entered:'.' '.$name.'<br
> />';
> }
>
> // check for email
> if(empty($_POST['email'])) {
> $email = FALSE;
> $messageProblem .= 'Please fill out your email address<br />';
> } elseif
> (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",
> (trim($_POST['email'])))) {
> $email = FALSE;
> $messageProblem .= 'Please enter a valid email address<br
> />';
> } else {
> $email = (trim($_POST['email']));
> $messageOK .= 'valid email entered:'.'
> '.$email.'<br />';
> }
>
> // check for email message
> if(empty($_POST['emailMessage'])) {
> $emailMessage = FALSE;
> $messageProblem .= 'Please complete your email message<br />';
> } else {
> $emailMessage = stripslashes(trim($_POST['emailMessage']));
> $messageOK .= 'Message entered:'.'
> '.$emailMessage.'<br />';
> }
>
>
>
> if ((strlen($name && $email && $emailMessage) > 0) &&
> ($captchaOk == true)) { // if all variables set
>
> require_once "Mail.php";
>
> $from = "**@******t.com";
> $to = "****@***********.com";
> $subject = 'email sent by:'.' '.$name;
> $body = 'from: ' . $email .'<p>'.
> $emailMessage .'<p/>';
>
> $host = "************";
> $username = "*******@********btinternet.com";
> $password = "*********";
>
>
> $headers = array ('From' => $from,'To' => $to,
> 'Subject' => $subject);
> $smtp = Mail::factory('smtp', array ('host' => $host,
> 'auth' => true, 'username' => $username, 'password' =>
> $password));
>
> $mail = $smtp->send($to, $headers, $body);
> if (PEAR::isError($mail)) {
> echo("<p>" . $mail->getMessage() .
> "</p>");
> } else {
> $mail = 'Message sent sucessfully<br />To send another message
> refresh the page';
>
> }
>
> } else {
> $mail = 'Message not sent';
>
> } // end of pear stmp send mail
>
> }// close of php form handler submit
>
> ?>
>
> <div id="container">
> <div id="menuIvy">
> <?php
> include('phpInc/ivyMenu_newSwapImage.inc');
> ?>
> </div>
> <div id="contactInfo">email
> <strong>info@michelleharrisonbooks.com</strong></div>
> <div id="phpContactWrapper">
> <div id="phpContact">
> <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
> method="post" >
> <strong>Email Michelle:</strong><br />
> Name<br />
> <input type="text" name="name" id="name"
> size="30" maxlength="40" value="<?php
> if(isset($_POST['name'])) echo $_POST['name']; ?>" />
> <br />
> Your email<br />
> <input type="text" name="email" id="email"
> size="30" maxlength="60" value="<?php
> if(isset($_POST['email'])) echo $_POST['email']; ?>" />
> <br />
> Message <br />
> <textarea type="text" name="emailMessage"
> id="emailMessage" cols="30" rows="5" value="" ><?php
> if(isset($_POST['emailMessage'])) echo $_POST['emailMessage'];
> ?></textarea>
> <br />
> Please enter the phrase in the image below: <br />
> <?php echo '<img src="' . md5(session_id()) . '.png?"
> />' ?>
> <br />
> <input type="text" name="captcha_phrase" />
> <p>
> <input name="submit" type="submit" value="send
> email" />
> </p>
> </form>
> </div>
> <!-- End of php contact div -->
> </div>
> <!-- End of php contact Wrapper div -->
> <div id="mailInfo">
> <?php echo stripslashes($messageProblem); echo stripslashes($messageOK); ?>
> </div>
> <div id="mailSentNot">
> <?php echo($captchaMessage); echo '<br />'; echo($mail); echo
> '<br />'; ?>
> </div>
> </div>
>
> </body>
> </html>
>
>
>
>
>
>