Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help with captcha refresh button

    I have written the captcha in php and the page it will be on is also php but can't seem to figure out how to write an onclick command for the refresh button. Can someone possibly post an example I could use to refresh the php code of the captcha?

    This is what I have
    <img src="captcha/captcha.php"/><img src='captcha/refresh.jpg' onClick="this.src='captcha.php?rand='+Math.random();">
    Last edited by IrishDoc; 10-11-2012 at 07:10 PM.

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    why would you refresh 'refresh button'? don't u want refresh captcha? ;>
    assign id to captcha image and then refresh it
    Code:
    <img src="captcha/captcha.php" id="captchaImg" />
    <img src="captcha/refresh.jpg" onclick="javascript:document.getElementById('captchaImg').src='captcha.php?rand='+new Date().getTime();" />
    also i wouldn't use random numbers. u can get the same result twice in a row. use 'new Date().getTime()'. more secure

  • Users who have thanked patryk for this post:

    IrishDoc (10-11-2012)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    also i wouldn't use random numbers. u can get the same result twice in a row. use 'new Date().getTime()'. more secure
    Thanks. What about getting new characters (fonts)?

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    it depends how you wrote that captcha.
    if you use imagettftext(), then u just upload ttf files to your server and use them.

    check my captca: http://photocaptcha.us/demo.php

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    ok, for some reason I am not getting an action from the refresh button.

    can you take a look at it.

    http://www.murderinc.net/page10.html

  • #6
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    because u have there
    Code:
    <img src="captcha/refresh.jpg" onclick="javascript:document.getElementById('captchaImg').src='captcha.php?rand='+new imagettftext();">
    instead of
    Code:
    <img src="captcha/refresh.jpg" onclick="javascript:document.getElementById('captchaImg').src='captcha.php?rand='+new Date().getTime();" />

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Sorry I should have mentioned that I wrote the code for fonts. I can post it if it will help. Do I need to add the getElementByID javascript, maybe???

  • #8
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    font thing is server side. you use it IN PHP CODE.
    refreshing captcha is done on client's side, so it's irrelevent here
    just put
    Code:
    onclick="javascript:document.getElementById('captchaImg').src='captcha.php?rand='+new Date().getTime();"
    in that refresh button and should work

    --edit--
    sorry. my bad.
    Code:
    onclick="javascript:document.getElementById('captchaImg').src='captcha/captcha.php?rand='+new Date().getTime();"
    Last edited by patryk; 10-11-2012 at 08:49 PM.

  • Users who have thanked patryk for this post:

    IrishDoc (10-11-2012)

  • #9
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Getting a reaction now but it is just removing the captcha image. I am so stuck. LOL

  • #10
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    look at edited post above. i've skipped path. sorry

  • Users who have thanked patryk for this post:

    IrishDoc (10-11-2012)

  • #11
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    now u have typo there. cptcha imstead to capttcha

  • #12
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    WOW!! Thanks for your help

  • #13
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    u might want to put that refresh button inside <a> tag, so that cursor would change when u hold it above it

  • Users who have thanked patryk for this post:

    IrishDoc (10-12-2012)

  • #14
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I am having to convert all this in wordpress for my customer. Can you show me what it is need to add the <a> to this code

    Code:
    $swat_res[] = '<img src="captcha/refresh.jpg" onclick="javascript:document.getElementById([\'captchaImg\']).src=[\'captcha/captcha.php?rand=\']+new Date().getTime();" />';
    Would it just be??

    Code:
    $swat_res[] = '<a img src="captcha/refresh.jpg" onclick="javascript:document.getElementById([\'captchaImg\']).src=[\'captcha/captcha.php?rand=\']+new Date().getTime();" <a/>';

  • #15
    New Coder
    Join Date
    Oct 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok I have got everything set but the captach isn't being verified. It allows the form to be submitted even if there is nothing inputed in the feild. Here is the code for the captcha.php file, and what I have entered into the php form, not sure what I have done wrong. Or if I just missed something I needed to add in. Am I correct that apache2 uses GD > 2.0 or am I just out of date, I haven't done this in a long time.

    Code:
    <?php 
    	// Captcha script for GD > 2.0
    
    /* 		Few notes about this script
    
    	the width and height are measured in px so adjust according to your desire
    	most variables in the variable section control the entire script so no editing below them should be needed
    	the session variable $_SESSION['tt_pass'] is what holds the pass in the session can be changed on line 45
    
    */
    
    
    /********************VARIABLES FOR THIS SCRIPT********************/
    /*********SHOULD BE THE ONLY THING YOU HAVE TO CHANGE*********/
    
    $pass_length = 5;					// passphrase length	
    $make_upper = true;					// all upper case letters in image? true or false
    $width = 200; $height = 60;			// image dimensions
    $font_path = dirname(__FILE__);		// TTF font @ only change this if you relocate the TTF files!
    $dark_font = true;					// if you want it easier to read, side effect bots can read it easier as well  true or false	
    	
    /********************END VARIABLES FOR SCRIPT********************/
    /*********         EDIT BELOW THIS AT YOUR OWN RISK :)           *********/
    
    
    
    
    
    	
    // create a passphrase  ** it is case sensitive!!! **
    	session_start();
    	$passwd = '';
    	$i = 0;
    	
    		while ($i < $pass_length) {
    			$passwd .= chr(rand(97, 122));
    			$i++;
    		}
    		
    	if ($make_upper) {
    		$passwd = strtoupper($passwd);
    	}
    
    // store the passphrase
    	$_SESSION['tt_pass'] = $passwd;
    	
    // get available fonts
    	$fonts = array();
    	
    		if ($handle = opendir($font_path)) {
    			while (false !== ($file = readdir($handle))) {
    				if (substr(strtolower($file), -4, 4) == '.ttf') {
    					$fonts[] = $font_path.'/'.$file;
    				}
    			}
    		}
    		if (count($fonts) < 1) {
    			die('No Fonts Found!!!');
    		}
    
    // image header
    	header("Content-Type: image/jpeg");
    
    // clear the cache
    	header("Expires: Fri, 09 Jan 2008 05:00:00 GMT");
    	header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
    	header("Cache-Control: no-store, no-cache, must-revalidate");
    	header("Cache-Control: post-check=0, pre-check=0", false);
    	header("Pragma: no-cache");
    	
    // create image
    	$img = imagecreatetruecolor($width, $height);
    	
    // fill background with random shade of pastel
    	$bg = imagecolorallocate($img, rand(210, 255), rand(210, 255), rand(210, 255));
    	
    // draw rectangle
    	imagefilledrectangle($img, 0, 0, $width, $height, $bg);
    
    // make the background jaggedy with differnet colored polygons
    	$right = rand(10, 30);
    	$left = 0;
    	
    		while ($left < $width) {
    			$poly_points = array(
    				$left, 0,
    				$right, 0,
    				rand($right-25, $right+25), $height,
    				rand($left-15, $left+15), $height);
    	
    			$c = imagecolorallocate($img, rand(210, 255), rand(210, 255), rand(210, 255));
    			imagefilledpolygon($img, $poly_points, 4, $c);
    			
    			$random_amount = rand(10, 30);
    			$left += $random_amount;
    			$right += $random_amount;
    		}
    		
    // base range for lines
    	$c_min = rand(150, 185);
    	$c_max = rand(195, 280);
    	
    // draw vertical lines
    	$left = 0;
    		while ($left < $width) {
    			$right = $left + rand(3, 7);
    			$offset = rand(-3, 3);
    			
    			$line_points = array(
    				$left, 0,
    				$right, 0,
    				$right + $offset, $height,
    				$left + $offset, $height);
    			
    			$pc = imagecolorallocate($img, rand($c_min, $c_max),
    										   rand($c_min, $c_max),
    										   rand($c_min, $c_max));
    			
    			imagefilledpolygon($img, $line_points, 4, $pc);
    			$left += rand(20, 60);
    		}
    	
    // draw horisontal lines
    	$top = 0;
    		while ($top < $height) {
    			$bottom = $top + rand(1, 4);
    			$offset = rand(-6, 6);
    			
    			$line_points = array(
    				0, $top,
    				0, $bottom,
    				$width, $bottom + $offset,
    				$width, $top + $offset);
    			$pc = imagecolorallocate($img, rand($c_min, $c_max),
    										   rand($c_min, $c_max),
    										   rand($c_min, $c_max));
    			imagefilledpolygon($img, $line_points, 4, $pc);
    			$top += rand(8, 15);
    		}
    		
    // character spacing
    	$spacing = $width/(strlen($passwd)+2);
    	$x = $spacing;
    
    // draw each character
    	for ($i=0; $i < strlen($passwd); $i++) {
    		$letter = $passwd[$i];
    		$size = rand($height/3, $height/2);
    		$rotation = rand(-30, 30);
    		$y = rand($height * .90, $height - $size - 4);
    	
    	// random font	
    		$font = $fonts[array_rand($fonts)];
    	
    	// color for letter
    	if ($dark_font) {	
    		$r = rand(0, 200); $g = rand(0, 200); $b = rand(0, 200);
    	} else {
    		$r = rand(100, 255); $g = rand(100, 255); $b = rand(100, 255);
    	}
    		
    	// create letter and shadow colors
    		$color = imagecolorallocate($img, $r, $g, $b);
    		$shadow = imagecolorallocate($img, $r/3, $g/3, $b/3);
    		
    	// draw the shadow than letter
    		imagettftext($img, $size, $rotation, $x, $y, $shadow, $font, $letter);
    		imagettftext($img, $size, $rotation, $x-1, $y-3, $color, $font, $letter);
    	
    	// space the letter
    		$x += rand($spacing, $spacing * 1.5);
    	}
    	
    	// clear the memory used to make the captcha image
    	imagejpeg($img);
    	imagedestroy($img);
    ?>
    this is in the form

    Code:
    </li>
    <li class="clearcontents">
    <label class="left"></label>
    Please confirm the letters in the image
    </li>
    <li class="clearcontents">
    <label class="left"></label>
    * All letters are case sensitive *
    </li>
    <li class="clearcontents">
    <label class="left"></label>
    <img id="captchaImg" src="captcha/captcha.php"/>
    <img src="captcha/refresh.jpg" onclick="javascript:document.getElementById([\'captchaImg\']).src=[\'captcha/captcha.php?rand=\']+new Date().getTime();" />
    </li>
    <li class="clearcontents">
    <label class="left"></label>
    Confirm Letters:
    <input type="text" id="tt_pass" name="tt_pass" style="width:100px;" maxlength="5"/>
    </li>
    and of course I added this to the header

    Code:
    <?php
    session_start(); ?>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •