Jump to content

Captcha problem (variable update problem) (Ajax)


Ifaiden

Recommended Posts

I've just followed this tutorial on how to make captcha:

http://www.webcheatsheet.com/PHP/create_captcha_protection.php

---

Here's how it looks like on my page:

http://81.226.101.66/projektarbete/benni/TEST/register.php

---

Now the problem is, the session variable "$_SESSION["security_code"]" that is created when the image is created in "create_image.php". When I run this file in the browser the variable does update to what is written in the image.

When I run the "register.php" the image refreshes, but the variable $_SESSION["security_code"] remains the same. I think that the problem is that the session variable isn't updated because "create_image.php" is never used

execept in<img id="imgCaptcha" src="captcha/create_image.php" >, that's only the img and not the code in that file. Please help!

---

Here's the code:

create_image.php

<?php

//Start the session so we can store what the security code actually is
session_start();

//Set the session to store the security code
$_SESSION["security_code"] = $security_code;

//Send a generated image to the browser 
create_image();
exit(); 

function create_image() 
{ 
global $security_code;

    //Let's generate a totally random string using md5 
    $md5_hash = md5(rand(0,999)); 
    //We don't need a 32 character long string so we trim it down to 5 
    $security_code = substr($md5_hash, 15, 5); 


    //Set the image width and height 
    $width = 100; 
    $height = 20;  

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray 
    $white = ImageColorAllocate($image, 255, 255, 255); 
    $black = ImageColorAllocate($image, 0, 0, 0); 
    $grey = ImageColorAllocate($image, 204, 204, 204); 

    //Make the background black 
    ImageFill($image, 0, 0, $black); 

    //Add randomly generated string in white to the image
    ImageString($image, 3, 30, 3, $security_code, $white); 

    //Tell the browser what kind of file is come in 
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format 
    ImageJpeg($image); 
    
    //Free up resources
    ImageDestroy($image); 
} 
?> 

part of register.php

 
<?php
include("include/session.php");
?>

<p>Anti-Spamskydd</p>
        <p>
        <img id="imgCaptcha" src="http://mindu.mine.nu/projektarbete/benni/TEST/captcha/create_image.php" >
        <?php echo $_SESSION["security_code"]; ?>


       <input id="btnCaptcha" type="button" value="Ladda om" name="btnCaptcha" 
					onclick="getParam(document.frmCaptcha)" /><br/>
        
        
        <input type="text" name="captcha" maxlength="30" value="<?php echo $form->value("captcha"); ?>"><?php echo $form->error("captcha"); ?>
        </p> 
        

        
	<p class="textinput"><input type="hidden" name="subjoin" value="1"><input type="submit" value="Registrera!"></p>

</form>

 

part of session.php

/* Captcha error checking */
$field = "captcha";

$captcha=$_REQUEST["captcha"];
$sec_code=$_SESSION["security_code"];


if ( ($_REQUEST["captcha"] == $_SESSION["security_code"]) && 
    (!empty($_REQUEST["captcha"]) && !empty($_SESSION["security_code"])) ) {
	$this->referrer = "http://81.226.101.66/projektarbete/benni/TEST/register.php"; // The captcha code is right, go back to register.php
}


else{
	if(!$subcaptcha || strlen($subcaptcha = trim($subcaptcha)) == 0){ // If the field is empty, display error
	$form->setError($field, "* skriv in kod $sec_code");
	$this->referrer = "http://81.226.101.66/projektarbete/benni/TEST/register.php";
	}

	else{
	$form->setError($field, "* Fel kod $captcha , $sec_code "); // Else the captcha is wrong, display error
	$this->referrer = "http://81.226.101.66/projektarbete/benni/TEST/register.php";
	}
} 

 

 

ajax_captcha.js

//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
} else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
} else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
}
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
}   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
//Check if our response is ready
if (receiveReq.readyState == 4) {
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'http://81.226.101.66/projektarbete/benni/TEST/captcha/create_image.php?' + Math.random();
}
}

//Called every time when form is perfomed
function getParam(theForm) {
//Set the URL
var url = 'include/session.php';
//Set up the parameters of our AJAX call
var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
//Call the function that initiate the AJAX request
makeRequest(url, postStr);
}

 

 

 

 

 

 

Link to comment
Share on other sites

Well just from looking at the top few lines I think your problem is that you're trying to store the $security_code before you've called the function that generates the security code..

 

//Set the session to store the security code
$_SESSION["security_code"] = $security_code;

//Send a generated image to the browser 
create_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.