Jump to content

Archived

This topic is now archived and is closed to further replies.

Mouse

Trying to be a bit too clever...

Recommended Posts

Where am i going wrong here?

ok i started with the Membership tutorial on PHP freaks... then i added a captcha from puremango.co.uk and it all whent pear shaped... here are the three scripts and if you can tell me why it's all gone pear shaped i'll bake you a cake...

Pritty Please

Mouse

Register.php
[code]
<?
include 'db.php';

$first_name = $_POST['firs_tname'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];

$first_name = stripslashes($firs_tname);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);


if((!$firs_tname) || (!$last_name) || (!$email_address) || (!$word)){
    echo 'You did not submit the following required information! <br />';
    if(!$firs_tname){
        echo "First Name is a required field. Please enter it below.<br />";
    }
    if(!$last_name){
        echo "Last Name is a required field. Please enter it below.<br />";
    }
    if(!$email_address){
        echo "Email Address is a required field. Please enter it below.<br />";
    }
   if(!$word){
       echo "The test word is a required field. Please enter it below.<br />";
   }
    include 'join_wrap.php';
    exit();
}

$sql_email_check = mysql_query("SELECT email_address FROM users
            WHERE email_address='$email'");

$email_check = mysql_num_rows($sql_email_check);

if(($email_check > 0)){
    echo "Please fix the following errors: <br />";
    if($email_check > 0){
        echo "<strong>Your email address has already been used by another member
        in our database. Please submit a different Email address!<br />";
        unset($email_address);
    }
    include 'join_form.htm';
    exit();  
}

function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000);
      $i = 0;
      while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($salt, $num, 1);
            $pass = $pass . $tmp;
            $i++;
      }
      return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

$sql = mysql_query("INSERT INTO users (firs_tname, last_name,
        email, password, signup_date)
        VALUES('$firs_tname', '$last_name', '$email_address',
        '$db_password', now())")
        or die (mysql_error());

if(!$sql){
    echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
    $userid = mysql_insert_id();
    
    $subject = "Your Membership at After the Mouse.com!";
    $message = "Dear $first_name,
    Thank you for registering at our website, http://www.afterthemouse.com!
    
    You are two steps away from logging in and accessing our exclusive members area.
    
    To activate your membership,  
    please click here: http://www.mysite.com/activate.php?id=$userid&code=$db_password
    
    Once you activate your memebership, you will be able to login
    with the following information:
    Password: $random_password
    
    Thanks!
    The Webmaster
    
    This is an automated response, please do not reply!";
    
    mail($email_address, $subject, $message,
        "From: After the Mouse.com  Webmaster<admin@afterthemouse.com>n
        X-Mailer: PHP/" . phpversion());
    echo 'Your membership information has been mailed to your email address!
    Please check it and follow the directions!';
}

?>
[/code]

then i added a captcha from puremango.co.uk

join_wrap.php
[code]
<?
//session_start();

if(!empty($_SESSION['freecap_word_hash']) && !empty($_POST['word']))
{
    if($_SESSION['hash_func'](strtolower($_POST['word']))==$_SESSION['freecap_word_hash'])
    {
        $_SESSION['freecap_attempts'] = 0;
        $_SESSION['freecap_word_hash'] = false;
        $word_ok = "yes";
    } else {
        $word_ok = "no";
    }
} else {
    $word_ok = false;
}
?>
<html>
<head>
<script language="javascript">
<!--
function new_freecap()
{
    if(document.getElementById)
    {
        thesrc = document.getElementById("freecap").src;
        thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
        document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
    } else {
        alert("Sorry, cannot autoreload word test image\nSubmit the form and a new word test will be loaded");
    }
}
//-->
</script>

<style type="text/css">
<!--
.style1 {font-size: x-small}
-->
</style>
</head>
<body>
<form method="post" action="register.php" onsubmit="return checkCheckBox(this)">
<table width="270" cellpadding="0" cellspacing="0">
<tr>
  <td width="76">Email:</td><td width="192"><input name="email" type="text" value="<?=htmlspecialchars($_POST['email'])?>" size="32"></td></tr>
<tr>
  <td width="76">Name:</td><td width="192"><input name="first_name" type="text" value="<?=htmlspecialchars($_POST['email'])?>" size="32"></td></tr>
<tr>
  <td>Surame:</td><td><input name="family_name" type="text" value="<?=htmlspecialchars($_POST['password'])?>" size="32"></td></tr>
<tr><td colspan="2"><img src="freecap.php" id="freecap"></td></tr>
<tr><td colspan="2"><div align="center" class="style1 style1">If you can't read the word, <a href="#" onClick="this.blur();new_freecap();return false;">click here</a></div></td>
</tr>
<tr>
  <td> Type word:</td>
  <td><input name="word" type="text" size="32"></td></tr>
<tr>
  <td colspan="2"><?
if($word_ok!==false)
{
    if($word_ok=="yes")
    {
        echo "you got the word correct.<br />";
    } else {
        echo "Sorry, that's not the right word, try again.<br />";
    }
}
?></td>
</tr>
<tr>
  <td colspan="2">I agree to the Terms & Conditions
    <input name="tandc" type="checkbox" id="tandc" value="on"></td>



</tr>
</table>

<input name="submit" type="submit" value="submit">
<br />
</form>
</body>
</html>
[/code]

and finally the captcha script

freecap.php (in need of a little trimming)
[code]
<?
session_start();
$site_tags[0] = "To avoid spam, please do NOT enter the text if";
$site_tags[1] = "this site is not After the Mouse.com";
$site_tags = null;
$tag_pos = 1;
$rand_func = "mt_rand";
$seed_func = "mt_srand";
$hash_func = "sha1";
$_SESSION['hash_func'] = $hash_func;
$output = "gif";
$use_dict = 0;
$max_word_length = 6;
$col_type = 1;
$max_attempts = 10;
$font_locations = Array("./.ht_freecap_font1.gdf","./.ht_freecap_font2.gdf","./.ht_freecap_font3.gdf","./.ht_freecap_font4.gdf","./.ht_freecap_font5.gdf");
$bg_type = 1;
$blur_bg = true;
$bg_images = Array("./.ht_freecap_im1.jpg","./.ht_freecap_im2.jpg","./.ht_freecap_im3.jpg","./.ht_freecap_im4.jpg","./.ht_freecap_im5.jpg");
    $merge_type = 1;
    $morph_bg = false;
$seed_func(make_seed());
switch($bg_type)
{
    case 0:
        break;
    case 1:
    case 2:
        $bg_fade_pct = 65;
        break;
    case 3:
        $bg_fade_pct = 50;
        break;
}
$bg_fade_pct += $rand_func(-2,2);
$font_widths = Array();
for($i=0; $i<sizeof($font_locations); $i++)
{
    $handle = fopen($font_locations[$i],"r");
    $c_wid = fread($handle,11);
    $font_widths[$i] = ord($c_wid{8})+ord($c_wid{9})+ord($c_wid{10})+ord($c_wid{11});
    fclose($handle);
}
$width = ($max_word_length*(array_sum($font_widths)/sizeof($font_widths))+75);
$height = 90;

$im = ImageCreate($width, $height);
$im2 = ImageCreate($width, $height);
if(empty($_SESSION['freecap_attempts']))
{
    $_SESSION['freecap_attempts'] = 1;
} else {
    $_SESSION['freecap_attempts']++;
    if($_SESSION['freecap_attempts']>$max_attempts)
    {
        $_SESSION['freecap_word_hash'] = false;

        $bg = ImageColorAllocate($im,255,255,255);
        ImageColorTransparent($im,$bg);

        $red = ImageColorAllocate($im, 255, 0, 0);
        ImageString($im,5,15,20,"service no longer available",$red);

        sendImage($im);
    }
}
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}

function rand_color() {
    global $bg_type,$rand_func;
    if($bg_type==3)
    {
        return $rand_func(10,100);
    } else {
        return $rand_func(60,170);
    }
}

function myImageBlur($im)
{
    $width = imagesx($im);
    $height = imagesy($im);

    $temp_im = ImageCreateTrueColor($width,$height);
    $bg = ImageColorAllocate($temp_im,150,150,150);
    ImageColorTransparent($temp_im,$bg);

    ImageFill($temp_im,0,0,$bg);

    $distance = 1;
    ImageCopyMerge($temp_im, $im, 0, 0, 0, $distance, $width, $height-$distance, 70);
    ImageCopyMerge($im, $temp_im, 0, 0, $distance, 0, $width-$distance, $height, 70);
    ImageCopyMerge($temp_im, $im, 0, $distance, 0, 0, $width, $height, 70);
    ImageCopyMerge($im, $temp_im, $distance, 0, 0, 0, $width, $height, 70);
    ImageDestroy($temp_im);

    return $im;
}

function sendImage($pic)
{
    global $output,$im,$im2,$im3;
    header(base64_decode("WC1DYXB0Y2hhOiBmcmVlQ2FwIDEuNCAtIHd3dy5wdXJlbWFuZ28uY28udWs="));
    switch($output)
    {
        case "jpg":
            header("Content-Type: image/jpeg");
            ImageJPEG($pic);
            break;
        case "gif":
            header("Content-Type: image/gif");
            ImageGIF($pic);
            break;
        case "png":
        default:
            header("Content-Type: image/png");
            ImagePNG($pic);
            break;
    }

    ImageDestroy($im);
    ImageDestroy($im2);
    ImageDestroy($pic);
    if(!empty($im3))
    {
        ImageDestroy($im3);
    }
    exit();
}
if($use_dict==1)
{
    $words = @file($dict_location);
    $word = strtolower($words[$rand_func(0,sizeof($words)-1)]);
    $word = ereg_replace("[^a-z]","",$word);
    $words = "";
    unset($words);
} else {
    $consonants = 'bcdghklmnpqrsvwxyz';
    $vowels = 'aeuo';
    $word = "";

    $wordlen = $rand_func(5,$max_word_length);

    for($i=0; $i<$wordlen; $i++)
    {
        if($rand_func(0,4)>=2 && $i!=0)
        {
            $word .= $vowels{$rand_func(0,strlen($vowels)-1)};
        } else {
            $word .= $consonants{$rand_func(0,strlen($consonants)-1)};
        }
    }
}
$_SESSION['freecap_word_hash'] = $hash_func($word);
$tag_col = ImageColorAllocate($im,10,10,10);
$site_tag_col2 = ImageColorAllocate($im2,0,0,0);
$debug = ImageColorAllocate($im, 255, 0, 0);
$debug2 = ImageColorAllocate($im2, 255, 0, 0);
$bg = ImageColorAllocate($im, 254, 254, 254);
$bg2 = ImageColorAllocate($im2, 254, 254, 254);

ImageColorTransparent($im,$bg);
ImageColorTransparent($im2,$bg2);

ImageFill($im,0,0,$bg);
ImageFill($im2,0,0,$bg2);

if($bg_type!=0)
{

    $im3 = ImageCreateTrueColor($width,$height);
    $temp_bg = ImageCreateTrueColor($width*1.5,$height*1.5);
    $bg3 = ImageColorAllocate($im3,255,255,255);
    ImageFill($im3,0,0,$bg3);
    $temp_bg_col = ImageColorAllocate($temp_bg,255,255,255);
    ImageFill($temp_bg,0,0,$temp_bg_col);
    if($bg_type==1)
    {
        for($i=$rand_func(6,20); $i<$width*2; $i+=$rand_func(10,25))
        {
            ImageSetThickness($temp_bg,$rand_func(2,6));
            $text_r = $rand_func(100,150);
            $text_g = $rand_func(100,150);
            $text_b = $rand_func(100,150);
            $text_colour3 = ImageColorAllocate($temp_bg, $text_r, $text_g, $text_b);

            ImageLine($temp_bg,$i,0,$i,$height*2,$text_colour3);
        }
        for($i=$rand_func(6,20); $i<$height*2; $i+=$rand_func(10,25))
        {
            ImageSetThickness($temp_bg,$rand_func(2,6));
            $text_r = $rand_func(100,150);
            $text_g = $rand_func(100,150);
            $text_b = $rand_func(100,150);
            $text_colour3 = ImageColorAllocate($temp_bg, $text_r, $text_g, $text_b);

            ImageLine($temp_bg,0,$i,$width*2, $i ,$text_colour3);
        }
    } else if($bg_type==2) {

        $bg3 = ImageColorAllocate($im3,255,255,255);
        ImageFill($im3,0,0,$bg3);
        ImageSetThickness($temp_bg,4);

        for($i=0; $i<strlen($word)+1; $i++)
        {
            $text_r = $rand_func(100,150);
            $text_g = $rand_func(100,150);
            $text_b = $rand_func(100,150);
            $text_colour3 = ImageColorAllocate($temp_bg, $text_r, $text_g, $text_b);

            $points = Array();
            for($j=1; $j<$rand_func(5,10); $j++)
            {
                $points[] = $rand_func(1*(20*($i+1)),1*(50*($i+1)));
                $points[] = $rand_func(30,$height+30);
            }

            ImagePolygon($temp_bg,$points,intval(sizeof($points)/2),$text_colour3);
        }

    } else if($bg_type==3) {

        for($i=0; $i<sizeof($bg_images); $i++)
        {
            $temp_im[$i] = ImageCreateFromJPEG($bg_images[$i]);
            $temp_width[$i] = imagesx($temp_im[$i]);
            $temp_height[$i] = imagesy($temp_im[$i]);
        }

        $blocksize = $rand_func(20,60);
        for($i=0; $i<$width*2; $i+=$blocksize)
        {
            for($j=0; $j<$height*2; $j+=$blocksize)
            {
                $image_index = $rand_func(0,sizeof($temp_im)-1);
                $cut_x = $rand_func(0,$temp_width[$image_index]-$blocksize);
                $cut_y = $rand_func(0,$temp_height[$image_index]-$blocksize);
                ImageCopy($temp_bg, $temp_im[$image_index], $i, $j, $cut_x, $cut_y, $blocksize, $blocksize);
            }
        }
        for($i=0; $i<sizeof($temp_im); $i++)
        {
            ImageDestroy($temp_im[$i]);
        }

    }
    if($morph_bg)
    {
        $morph_chunk = $rand_func(1,5);
        $morph_y = 0;
        for($x=0; $x<$width; $x+=$morph_chunk)
        {
            $morph_chunk = $rand_func(1,5);
            $morph_y += $rand_func(-1,1);
            ImageCopy($im3, $temp_bg, $x, 0, $x+30, 30+$morph_y, $morph_chunk, $height*2);
        }

        ImageCopy($temp_bg, $im3, 0, 0, 0, 0, $width, $height);

        $morph_x = 0;
        for($y=0; $y<=$height; $y+=$morph_chunk)
        {
            $morph_chunk = $rand_func(1,5);
            $morph_x += $rand_func(-1,1);
            ImageCopy($im3, $temp_bg, $morph_x, $y, 0, $y, $width, $morph_chunk);

        }
    } else {
        ImageCopy($im3,$temp_bg,0,0,30,30,$width,$height);
    }

    ImageDestroy($temp_bg);

    if($blur_bg)
    {
        myImageBlur($im3);
    }
}
$word_start_x = $rand_func(5,32);
$word_start_y = 15;
if($col_type==0)
{
    $text_r = rand_color();
    $text_g = rand_color();
    $text_b = rand_color();
    $text_colour2 = ImageColorAllocate($im2, $text_r, $text_g, $text_b);
}

for($i=0; $i<strlen($word); $i++)
{
    if($col_type==1)
    {
        $text_r = rand_color();
        $text_g = rand_color();
        $text_b = rand_color();
        $text_colour2 = ImageColorAllocate($im2, $text_r, $text_g, $text_b);
    }

    $j = $rand_func(0,sizeof($font_locations)-1);
    $font = ImageLoadFont($font_locations[$j]);
    ImageString($im2, $font, $word_start_x+($font_widths[$j]*$i), $word_start_y, $word{$i}, $text_colour2);
}
$font_pixelwidth = $font_widths[$j];
$word_pix_size = $word_start_x+(strlen($word)*$font_pixelwidth);

for($i=$word_start_x; $i<$word_pix_size; $i+=$font_pixelwidth)
{
    $prev_y = $y_pos;
    do{
        $y_pos = $rand_func(-5,5);
    } while($y_pos<$prev_y+2 && $y_pos>$prev_y-2);
    ImageCopy($im, $im2, $i, $y_pos, $i, 0, $font_pixelwidth, $height);
}
ImageFilledRectangle($im2,0,0,$width,$height,$bg2);
$y_chunk = 1;
$morph_factor = 1;
$morph_x = 0;
for($j=0; $j<strlen($word); $j++)
{
    $y_pos = 0;
    for($i=0; $i<=$height; $i+=$y_chunk)
    {
        $orig_x = $word_start_x+($j*$font_pixelwidth);
        $morph_x += $rand_func(-$morph_factor,$morph_factor);
        ImageCopyMerge($im2, $im, $orig_x+$morph_x, $i+$y_pos, $orig_x, $i, $font_pixelwidth, $y_chunk, 100);
    }
}
ImageFilledRectangle($im,0,0,$width,$height,$bg);
$y_pos = 0;
$x_chunk = 1;
for($i=0; $i<=$width; $i+=$x_chunk)
{
    $y_pos += $rand_func(-1,1);
    ImageCopy($im, $im2, $i, $y_pos, $i, 0, $x_chunk, $height);
}
myImageBlur($im);
if($output!="jpg" && $bg_type==0)
{
    ImageColorTransparent($im,$bg);
}
ImageFilledRectangle($im2,0,0,$width,$height,$bg2);
if(is_array($site_tags))
{
    for($i=0; $i<sizeof($site_tags); $i++)
    {
        $tag_width = strlen($site_tags[$i])*6;
        if($tag_pos==0 || $tag_pos==2)
        {
            ImageString($im2, 2, intval($width/2)-intval($tag_width/2), (10*$i), $site_tags[$i], $site_tag_col2);
        }
        if($tag_pos==1 || $tag_pos==2)
        {
            ImageString($im2, 2, intval($width/2)-intval($tag_width/2), ($height-34+($i*10)), $site_tags[$i], $site_tag_col2);
        }
    }
}
ImageCopyMerge($im2,$im,0,0,0,0,$width,$height,80);
ImageCopy($im,$im2,0,0,0,0,$width,$height);
if($bg_type!=0)
{
    if($bg_type!=3)
    {
        $temp_im = ImageCreateTrueColor($width,$height);
        $white = ImageColorAllocate($temp_im,255,255,255);
        ImageFill($temp_im,0,0,$white);
        ImageCopyMerge($im3,$temp_im,0,0,0,0,$width,$height,$bg_fade_pct);
        ImageDestroy($temp_im);
        $c_fade_pct = 50;
    } else {
        $c_fade_pct = $bg_fade_pct;
    }
    if($merge_type==1)
    {
        ImageCopyMerge($im3,$im,0,0,0,0,$width,$height,100);
        ImageCopy($im,$im3,0,0,0,0,$width,$height);
    } else {
        ImageCopyMerge($im,$im3,0,0,0,0,$width,$height,$c_fade_pct);
    }
}
$tag_str = "After the Mouse.Com";
$tag_width = strlen($tag_str)*6;
ImageString($im, 2, $width-$tag_width, $height-13, $tag_str, $tag_col);
unset($word);
unset($use_dict);
unset($dict_location);
unset($max_word_length);
unset($bg_type);
unset($bg_images);
unset($merge_type);
unset($bg_fade_pct);
unset($morph_bg);
unset($col_type);
unset($max_attempts);
unset($font_locations);
sendImage($im);
?>
[/code]


Share this post


Link to post
Share on other sites
Hi Mouse did you ever succeed here?  I too am tryng to integrate captcha into the membership tutorial but failing badly.  Any help greatly appreciated as the spam is starting to bury me >:( !!!!

Share this post


Link to post
Share on other sites
1. in join_warp.php you have input name="family_name" and you look for $_POST['last_name'] in register.php

2. you don't set up $word = $_POST['word'] in register.php

3. you must chck if $word is OK this is first part of join_warp.php (16 lines)

Share this post


Link to post
Share on other sites

×

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.