• home
  • forum
  • my
  • kt
  • download
  • Simple counter

    Author: 2008-08-16 08:36:20 From:

    Okay here's our code of this captcha image:

    Code: Select all
    <?php
    header('Content-type: image/jpg');
    $im = imagecreatefromjpeg ("image.jpg");
    $rand1 = rand(1,255);
    $rand2 = rand(1,255);
    $rand3 = rand(1,255);
    $color = imagecolorallocate($im, $rand1, $rand2, $rand3);
    $text = rand(1000, 9999);
    $font = 'text-font.otf';
    $size = 30;
    imagettftext($im, $size, 0, 70, 45, $color, $font, $text);
    imagejpeg($im);
    imagedestroy($im);
    ?>
    But if we want to use this image on our sites we must add:

    Code: Select all
    $_SESSION['captcha'] = $text;

    on the bottom of our captcha file. So.. now let's make our form secure.

    First of all we must start the session

    Code: Select all
    <?php
    session_start();


    session_start(); - Starts the session

    Now I'm using a form for checking that is the security code correct

    Code: Select all
    if(isset($_POST['submit'])){
    $ses = $_SESSION['captcha'];
    if($ses == $_POST['code']){
    echo "Code is correct!";
    }else{
    echo "Code is incorrect!";
    }


    if(isset($_POST['submit']) && !empty($_POST['code'])){ - If someone has clicked the submit button let's continue with our script
    $ses = $_SESSION['captcha']; - Let's get the captcha code from the session
    if($ses == $_POST['code']){ - If captcha's code is same as the code you posted
    echo "Code is correct!"; - let's say that the code was correct
    }else{ - but if they are not same
    echo "Code is incorrect!"; - let's say that the code is not correct
    } - This ends the if and else statement

    Code: Select all
    }else{
    echo "<img src='captcha.php'><br>";
    echo "<form action='' method='post'>"
        ."<input type='text' name='code'>"
        ."<input type='submit' name='submit' value='Enter'>"
        ."</form>";
    }


    }else{ - If anyone hasnt clicked the submit button let's display our form
    echo "<img src='captcha.php'><br>"; - Shows our captcha image
    echo "<form action='' method='post'>"
    ."<input type='text' name='code'>"
    ."<input type='submit' name='submit' value='Enter'>"
    ."</form>";
    - Our form
    } - This ends the if and else statement

    code:%20select%20all%20
    <?phpsession_start();$im%20=%20imagecreatefromjpeg ("image.jpg");
    $rand1 = rand(1,255);
    $rand2 = rand(1,255);
    $rand3 = rand(1,255);
    $color = imagecolorallocate($im, $rand1, $rand2, $rand3);
    $text = rand(1000, 9999);
    $font = 'text-font.otf';
    $size = 30;
    imagettftext($im, $size, 0, 70, 45, $color, $font, $text);
    header('Content-type: image/jpg');
    imagejpeg($im);
    imagedestroy($im);
    $_SESSION['captcha'] = $text;
    ?>


    index.php

    Code: Select all
    <?php
    session_start();

    if(isset($_POST['submit']) && !empty($_POST['code'])){
    $ses = $_SESSION['captcha'];
    if($ses == $_POST['code']){
    echo "Code is correct!";
    }else{
    echo "Code is incorrect!";
    }
    }else{
    echo "<img src='captcha.php'><br>";
    echo "<form action='' method='post'>"
        ."<input type='text' name='code'>"
        ."<input type='submit' name='submit' value='Enter'>"
        ."</form>";
    }
    ?>

    Enjoy!

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot