1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

My First Program In Php Using Array

Discussion in 'PHP' started by alexinawhite, Feb 10, 2013.

  1. #1
    Hi friend i develop a simple captcha code in PHP using array. Please take a look tell me how is it ? I know there are a lot of ready-made programs available on the net which 100 times better then my program , but it is developed myself and all you know the code which is written by yourself you like that most.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <?php
     
    if($_REQUEST["varify"])
    {
       
        $value=$_REQUEST["hidden"];
        $match=$_REQUEST["code"];   
       
        if($value==$match)
        {
            echo "value matched";
           
            }
           
            else
            {
                echo "value NOOOT  matched";
               
                } 
       
        }
     
     
    $number1=range('a','z');
    $number2=range(0,9);
    $number3=range('a','z');
    $number4=range('a','z');
    $number5=range('a','z');
     
    $number6=range('a','z');
    $number7=range(0,9);
    $number8=range(0,9);
    $number9=range('a','z');
    $number10=range('a','z');
     
    $fontface=array("Algerian","Bauhaus 93","Blackadder ITC","Brush Script MT","Chiller","Curlz MT","Fixedsys","Forte","French Script MT","Gigi");
     
    $number1r=array_rand($number1);
    $number2r=array_rand($number2);
    $number3r=array_rand($number3);
    $number4r=array_rand($number4);
    $number5r=array_rand($number5);
     
    $number6r=array_rand($number6);
    $number7r=array_rand($number7);
    $number8r=array_rand($number8);
    $number9r=array_rand($number9);
    $number10r=array_rand($number10);
     
    $fontfacer=array_rand($fontface);
     
     
    $captcha=$number1[$number1r].$number2[$number2r].$number3[$number3r].$number4[$number4r].$number5[$number5r].$number6[$number6r].$number7[$number7r].$number8[$number8r].$number9[$number9r].$number10[$number10r];
    ?>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form method="post" action="" name="1">
    <div style="background-color:#3C6; height:100px; width:350px; font-size:60px; text-align:center; font-family:<?Php echo $fontface[$fontfacer];?>;">
    <?php  echo $captcha; ?>
    </div>
    <div style="height:35px; width:350px; background-color:#F90;"><input type="text" name="code" style="width:230px; height:20px; margin-left:35px; margin-top:4px;"/> <input type="hidden" name="hidden" value="<?php echo $captcha; ?>"  /><input type="submit" name="varify" value="Varify"/></div>
    </form>
    </body>
    </html>
    Code (markup):

     
    Last edited by a moderator: Feb 10, 2013
    alexinawhite, Feb 10, 2013 IP
  2. dixcoder

    dixcoder Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #2
    really cool .. I like it
    suggestion
    instead of
    if($_REQUEST["varify"])
    please change to
    if(isset($_REQUEST["varify"]))
    thanks
     
    dixcoder, Feb 11, 2013 IP
  3. alexinawhite

    alexinawhite Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3

    thanks for suggestion ... :)
     
    Last edited by a moderator: Feb 11, 2013
    alexinawhite, Feb 11, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Some advice:

    1) It's 2013, lose the tranny.

    2) some function calls could greatly reduce the overall size.

    3) Making your indents actually line up could go a LONG ways for code clarity.

    4) Avoid using $_request -- it's generally frowned upon as sloppy.

    5) even in test code, stop inlining the CSS. That IS sloppy coding (honestly I say STYLE should be obsoleted as a tag and deprecated as an attribute)

    6) Whiskey tango foxtrot does "varify" mean?!?

    7) It's often easier/cleaner to only use <?php ?> once (if I had my way <?php ?> would be removed from the language entirely!)

    This is a bit more random -- any styling that's 'missing' belongs in the external stylesheet.

    <?php
     
    if (isset($_POST["verify"])) {
    	if($_POST["hidden"]==$_POST["code"]) {
    		echo "value matched";
    	} else {
    		echo "value NOT matched";
    	} 
    }
    
    $captcha='';
    for ($t=0; $t<10; $t++) {
    	switch (rand(1,3)) {
    		case 1:
    			$captcha.=chr(rand(48,57));
    		break;
    		case 2:
    			$captcha.=chr(rand(65,90));
    		break;
    		case 3:
    			$captcha.=chr(rand(97,122));
    		break;
    	}
    }
     
    $fontList = array(
    	"Algerian", "Bauhaus 93", "Blackadder ITC", "Brush Script MT", "Chiller",
    	"Curlz MT", "Fixedsys", "Forte", "French Script MT", "Gigi"
    );
    
    $fontFace=array_rand($fontface);
    
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html
    	xmlns="http://www.w3.org/1999/xhtml"
    	lang="en"
    	xml:lang="en"
    ><head>
    
    <meta
    	http-equiv="Content-Type"
    	content="text/html; charset=utf-8"
    />
    
    <link
    	type="text/css"
    	rel="stylesheet"
    	href="screen.css"
    	media="screen,projection,tv"
    />
     
    <title>
    	Captcha Demo
    </title>
    
    </head><body>
    
    <form method="post" action="" name="1">
    	<div style="font-family:',$fontface,'">
    		',$captcha,'
    	</div>
    	<fieldset>
    		<input type="text" name="code" id="code" />
    		<input type="submit" name="verify" value="verify" />
    		<input type="hidden" name="hidden" value="',$captcha,'" />
    	</fieldset>
    </form>
    
    </body></html>';
    
    ?>
    Code (markup):
    NOT that a captcha should be sending the value as a hidden client side... Nor should it be built from cData regardless of how random it is. :D
     
    deathshadow, Feb 11, 2013 IP
  5. alexinawhite

    alexinawhite Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5


    Good "deathshadow" but when i run your code one there is 2 issues to solve:
    1. warning showing : "Warning: array_rand() expects parameter 1 to be array, null given"

    2. font face is not working.
     
    alexinawhite, Feb 11, 2013 IP
  6. tribulant

    tribulant Active Member

    Messages:
    352
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Good work, well done!

    You're creating random numbers and also a random font face, looks good.

    Just check, I know that some bots can read text like font.
     
    tribulant, Feb 11, 2013 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Oops, two typo's.

    <?php
     
    if (isset($_POST["verify"])) {
    	if($_POST["hidden"]==$_POST["code"]) {
    		echo "value matched";
    	} else {
    		echo "value NOT matched";
    	} 
    }
    
    $captcha='';
    for ($t=0; $t<10; $t++) {
    	switch (rand(1,3)) {
    		case 1:
    			$captcha.=chr(rand(48,57));
    		break;
    		case 2:
    			$captcha.=chr(rand(65,90));
    		break;
    		case 3:
    			$captcha.=chr(rand(97,122));
    		break;
    	}
    }
     
    $fontList = array(
    	"Algerian", "Bauhaus 93", "Blackadder ITC", "Brush Script MT", "Chiller",
    	"Curlz MT", "Fixedsys", "Forte", "French Script MT", "Gigi"
    );
    
    $fontFace=array_rand($fontList);
    
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html
    	xmlns="http://www.w3.org/1999/xhtml"
    	lang="en"
    	xml:lang="en"
    ><head>
    
    <meta
    	http-equiv="Content-Type"
    	content="text/html; charset=utf-8"
    />
    
    <link
    	type="text/css"
    	rel="stylesheet"
    	href="screen.css"
    	media="screen,projection,tv"
    />
     
    <title>
    	Captcha Demo
    </title>
    
    </head><body>
    
    <form method="post" action="" name="1">
    	<div style="font-family:',$fontFace,'">
    		',$captcha,'
    	</div>
    	<fieldset>
    		<input type="text" name="code" id="code" />
    		<input type="submit" name="verify" value="verify" />
    		<input type="hidden" name="hidden" value="',$captcha,'" />
    	</fieldset>
    </form>
    
    </body></html>';
    
    ?>
    Code (markup):
    What I get for not TESTING code before posting and just typing it into the quick reply.
     
    deathshadow, Feb 11, 2013 IP
  8. alexinawhite

    alexinawhite Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    No problem deathshadow, and thanks for giving me your valuable suggestion. I will follow you for learning real development skills.
     
    alexinawhite, Feb 12, 2013 IP
  9. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #9
    There are two things here I think you misunderstood about how captcha works.
    1. Captcha should be genereted to some kind of media (images, sound) that people can read but auto-posting programs (aka. bots) find hard to read.
    2. The value for verifying the captcha should not be store right there in the hidden input. You can store in Database, SESSION or put the hash value (md5) value there.
    Anyway, it's a good try though.
     
    goldensea80, Feb 13, 2013 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Exactly what I meant by sending a value client-side as C-Data, either way. Hoping this is a learning exercise, not something someone would actually try to use as a captcha.
     
    deathshadow, Feb 13, 2013 IP