Web Analytics Made Easy -
StatCounter Please help on creating image verification code - CodingForum

Announcement

Collapse
No announcement yet.

Please help on creating image verification code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Please help on creating image verification code

    Hi,
    Please i'm writing a code to generate a code foe the user to enter and submit..the process work fine but when i use imageline() function to
    to generate a distort line to make the random numbers loop a bit difficult, it does not work

    I mean the image line i added does not make the code work at all and even the codes not show but when i comment out the
    imagelin() part then it works..please here is the code and help fix the imageline() part for me..i have generate.php page and index.php page

    under generate.[php] page code here

    [<?php]

    session_start();
    header('content-type: image/jpeg');

    //we first need to stored it in a session
    $text=$_SESSION['secure'];

    $font_size=30; //after storing it in a session,we give a font size

    $image_width=130; //we then find the image width and the image height
    $image_height=40;

    //we the n create our image using imagecreate function
    $image=imagecreate($image_width,$image_height);

    //we then give the bgcolor and the font color for the image using a function called imagecolorallocate
    imagecolorallocate($image,255,255,255);

    $font_color=imagecolorallocate($image,0,0,0);

    //here i will use imageline function to add a little distort to the image to make it difficult to read
    //i will use for loop to achiev that
    for($x=0; $x<=30, $x++){
    $x1=rand(1,100);
    $y1=rand(1,100);
    $x2=rand(1,100);
    $y2=rand(1,100);

    imageline($image,$x1,$y1,$x2,$y2,$font_color);

    }
    //we then also use a function called imagettftext function which takes 10 arqument.
    imagettftext($image,$font_size,0,15,35,$font_color,'font.ttf',$text);

    imagejpeg($image);
    [?>]


    under index.[php] code here

    [<?php]


    session_start();
    if(!isset($_SESSION['secure'])){
    $_SESSION['secure']=rand(1000,9999);

    }else if($_SESSION['secure']==$_GET['secure']){
    echo 'A match';
    }else{
    echo 'incorrect code,try again';
    $_SESSION['secure']=rand(1000,9999);
    }

    [?>]
    <br><br>
    <img src="generate.php"/>

    <form action="index.php" method="GET">
    Type the verification code: <input type="text" size="6" name="secure"> <input type="submit" value="Verify">

    </form>

    Please help
    Thanks
    Clement Osei

  • #2
    Your code has a syntax error. You should replace , with ;
    for($x=0; $x<=30, $x++)

    You can add ob_start(); right after <?php and

    PHP Code:
    ob_clean();
    header('Content-Type: image/jpeg');
    imagejpeg($image); 
    at the end of the script.
    You should also check that font.ttf exists and is accessible by the script.

    ps. For better code readability, you should use [PHP] tags.

    Comment


    • #3
      PHP Code:
      for($x=0$x<=30$x++){ 
      Loops are controlled by three separate instructions. You only have two, so the problem here will be a syntax error.

      In the future make sure you wrap all code with [php][/php] or [code][/code] tags.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment


      • #4
        Hi to you all,
        Thanks very much for correcting me this error.

        Now can you help me on how to mix numbers and letters, i try this and is not working
        it is giving me error undefined text_display

        Please check the code..under generate.[php] page

        [<?php]

        session_start();
        header('content-type: image/jpeg');

        //we first need to stored it in a session
        $text=$_SESSION['secure'];

        $font_size=30; //after storing it in a session,we give a font size

        $image_width=250; //we then find the image width and the image height
        $image_height=40;

        //we the n create our image using imagecreate function
        $image=imagecreate($image_width,$image_height);

        //we then give the bgcolor and the font color for the image using a function called imagecolorallocate
        imagecolorallocate($image,255,255,255);

        $font_color=imagecolorallocate($image,0,0,0);

        //here i will use imageline function to add a little distort to the image to make it difficult to read
        //i will use for loop to achiev that
        for($x=0; $x<=30; $x++){
        $x1=rand(1,100);
        $y1=rand(1,100);
        $x2=rand(1,100);
        $y2=rand(1,100);

        imageline($image,$x1,$y1,$x2,$y2,$font_color);

        }
        //we then also use a function called imagettftext function which takes 10 arqument.
        imagettftext($image,$font_size,0,15,35,$font_color,'font.ttf',$text);

        imagejpeg($image);
        [?>]


        and under index.[php] page

        [<?php]


        session_start();
        if(!isset($_SESSION['secure'])){

        $text='ABCDEFGHIJKLmonpjrst0123456789';
        $text_shuffle=str_shuffle($text);
        $text_display=substr($text_shuffle,0,8);
        $_SESSION['secure']=$text_display;

        }else if($_SESSION['secure']==$_GET['secure']){
        echo 'A match';
        }else{
        echo 'incorrect code,try again';
        $_SESSION['secure']=$text_display;
        }

        [?>]
        <br><br>
        <img src="generate.php"/>

        <form action="index.php" method="GET">
        Type the verification code: <input type="text" size="6" name="secure"> <input type="submit" value="Verify">

        </form>

        Please help

        Clement Osei

        Comment


        • #5
          $text_display is not defined in the scope of the else, so the only time you can use it is when there is no 'secure' defined in session. Make sure you're removing it as well when the transaction is complete.

          I will say again: In the future make sure you wrap all code with or tags.
          PHP Code:
          header('HTTP/1.1 420 Enhance Your Calm'); 
          Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎