Jump to content

Image reload


slpctrl

Recommended Posts

Hi everyone, I have this PHP CAPTCHA here:

 

Here is the file to create the image:

 

<?php
session_start();
$str1 = md5(microtime() * mktime());
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$black = imagecolorallocate($captcha,0,0,0);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(50,200),rand(0,100),rand(50,200),rand(80,200),$black);
imageline($captcha,rand(50,250),rand(0,100),rand(0,200),rand(50,200),$black);
imageline($captcha,rand(70,150),rand(0,100),rand(50,200),rand(0,200),$black);
imageline($captcha,rand(20,150),rand(0,100),rand(20,150),rand(35,200),$black);
imageline($captcha,rand(80,200),rand(0,100),rand(50,200),rand(85,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imagestring($captcha,rand(1,5),rand(0,100),rand(0,50),$str,$black);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

 

And my HTML form with the script to check the user input to see if it was the text created in the image:

 

<?php
session_start();
echo <<<HTML
<html>
<head>
<title>Captcha</title>
</head>
<body>
<img src="captcha.php" border="0">
<form action="" method="post">
Enter the text from the image:<br><input type="text" name="code" />
<input type="submit" />
</form>
</body>
</html>
HTML;
if(!$_POST['code'])
die();
else
{
if(md5($_POST['code']) != $_SESSION['str'])
die("Error: Wrong code entered");
else
echo("Correct!");
}
?>

 

Alright, now in the form though I want a button to ONLY refresh the image, not the whole page. How would I go about doing this?

Link to comment
Share on other sites

<input type="button" name="refresh" id="refresh" value="Refresh Captcha" onclick="javascript: document.getElementById("captcha").location='captcha.php?rand='+Math.random();" />

 

Then just give you captcha.php image the id of "captcha". The ?rand= just makes it get a new image.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.