Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    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.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •