[NEED HELP] Simple captcha integration

Discussion in 'PHP' started by proxywhereabouts, Feb 3, 2009.

  1. #1
    I have these codes.

    image.php
    <?php
    session_start();
    //CAPTCHA GENERATOR
    //Created by Rob Valkass 2007
    //Edited by QuickSilva
    //Feel free to distribute and use as you want
    //Just leave this notice and comments intact
    
    //Email: webmaster@rvalkass.co.uk
    //Web: www.rvalkass.co.uk
    
    
    //Set the header to say what sort of information we are giving the browser
    Header ("(anti-spam-content-type:) image/png");
    
    //Generate 2 random numbers for use in our encryption
    $enc_num = rand(0, 9999); //This number will be encrypted
    $key_num = rand(0, 24); //This is used to choose which bit of our string to use in the image
    
    //Use these to get a random string of numbers and letters using MD5
    //We then cut the 32 char MD5 down to an 8 char string, starting from a random point
    $hash_string = substr(md5($enc_num), $key_num, 8);
    
    //MD5 the $hash_string variable again
    $hash_md5 = md5($hash_string);
    
    //Asign it to a session
    $_SESSION['captcha'] = $hash_md5;
    
    //Create an array of the images available to us as backgrounds
    $bgs = array("lipsum.png", "fibres.png", "rainbow.png");
    
    //Choose the background image using the handy array_rand function
    $background = array_rand($bgs, 1);
    
    //Now to start creating the all important image!
    //Set the background as our randomly selected image
    $img_handle = imagecreatefrompng($bgs[$background]);
    
    //Set our text colour to white
    $text_colour = imagecolorallocate($img_handle, 255, 255, 255);
    
    //Set the font size
    $font_size = 5;
    
    //Get the horizontal and vertical dimensions of the background image
    $size_array = getimagesize($bgs[$background]);
    $img_w = $size_array[0];
    $img_h = $size_array[1];
    
    //Work out the horizontal position
    $horiz = round(($img_w/2)-((strlen($hash_string)*imagefontwidth(5))/2), 1);
    
    //Work out the vertical position
    $vert = round(($img_h/2)-(imagefontheight($font_size)/2));
    
    //Put our text onto our image
    imagestring($img_handle, $font_size, $horiz, $vert, $hash_string, $text_colour);
    
    //Output the image
    imagepng($img_handle);
    
    //Destroy the image to free up resources
    imagedestroy($img_handle);
    
    ?>
    PHP:

    process.php
    <?php
    session_start(); //Start the session
    $session = $_SESSION['captcha']; //Define the session we set in image.php
    $image = $_POST['image']; //Define the post
    $image = md5($image); //MD5 encrypt the post
    
    if ($session == $image){ //if they have put the right text in
    echo "Correct!";
    }else{
    echo "Incorrect!";
    }
    ?>
    PHP:

    form.php
    <form method="post" action="process.php">
    <img src="image.php" border="0"><br />
    <input type="text" name="image"><br />
    <input type="submit" value="Submit">
    </form>
    PHP:

    Submission page:
    <script language="JavaScript">
            function check(){
                    if (document.form.url.value=='' || document.form.url.value=='http://www.' || document.form.email.value==''){
                            alert('Please fill empty fields!'); return false;
                    }
            }
    </script>
    
    <form method="post" action="" name="form" onSubmit="return check();">
    
    <strong>Website URL:</strong>
    <div align=center><input type=text name=url value='http://www.' style="width: 350px; height: 20px; background-color:#DDDDCB" ></div>
    <strong>Category:</strong>
    <div align=center><select name="category" style="width: 350px; height: 20px; background-color:#DDDDCB" >
    <?php
    $cat_list=@file("categories.txt");
    $count_cat=count($cat_list);
    
    for ($i=0;$i<$count_cat;$i++){
    echo <<<D
            <option value="$cat_list[$i]">$cat_list[$i]</option>
    D;
    }
    ?>
    </select></div>
    
    <strong>Email:</strong>
    <div align=center><input type=text name=email style="width: 350px; height: 20px; background-color:#DDDDCB" ></div>
    <br />
    <div align=center><input type=submit value='Submit'></div>
    </form>
    PHP:

    I want to integrate the above codes into my submission page. Tried it already but it seems the I missed something in that process.php. Something that make the script send user to the thank you page if the code accepted or back to submit page if wrong code entered.

    Can anyone help me?
     
    proxywhereabouts, Feb 3, 2009 IP
  2. proxywhereabouts

    proxywhereabouts Notable Member

    Messages:
    4,027
    Likes Received:
    110
    Best Answers:
    0
    Trophy Points:
    200
    #2
    Can anyone provide some help with this?
    It would be much much much appreciated and will be repped.

    Thanks.
     
    proxywhereabouts, Feb 4, 2009 IP
  3. proxywhereabouts

    proxywhereabouts Notable Member

    Messages:
    4,027
    Likes Received:
    110
    Best Answers:
    0
    Trophy Points:
    200
    #3
    olddocks, the code which I posted ARE open source.
    I'm ust asking for a way to correctly integrate the code into my submission page.
    And you spam this thread with your site. So, I reported you.
     
    proxywhereabouts, Feb 4, 2009 IP
  4. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    swapshop, Feb 4, 2009 IP
  5. proxywhereabouts

    proxywhereabouts Notable Member

    Messages:
    4,027
    Likes Received:
    110
    Best Answers:
    0
    Trophy Points:
    200
    #5
    Thanks for the suggestion. Although it is not what I'm looking for, I've download it and will look into its source code to see its implementation.
     
    proxywhereabouts, Feb 5, 2009 IP