• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create Captcha in PHP

splizzer

Binary Builder
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
73
Likes
79
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this article, we are going to create Captcha using PHP. This simple article will help you on how to create this simple captcha with this simple source code. Enable you to generate a random code and you can use this to protect your page from random spammers. You can use this also as a type of a simple test that the response is generated by a human being and it's a very common on other websites.

Creating random code, TextBox, and one button as shown in the image below.
3_9.png

Here's the short source code for the image above.
  1. <form

    class

    =

    "form-horizontal"

    method

    =

    "POST"

    >

  2. <img

    src

    =

    "generatecaptcha.php?rand=<?php echo rand(); ?>

    " id='image_captcha' >
  3. <a

    href

    =

    'javascript: refreshing_Captcha();'

    ><i

    class

    =

    "icon-refresh icon-large"

    ></

    i

    ></

    a

    >
  4. <script

    language

    =

    'JavaScript'

    type

    =

    'text/javascript'

    >
  5. function refreshing_Captcha()
  6. {
  7. var img = document.images['image_captcha'];
  8. img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  9. }
  10. </

    script

    >

  11. <div

    class

    =

    "control-group"

    >
  12. <label

    class

    =

    "control-label"

    for

    =

    "inputPassword"

    >

    Enter the Code Above</

    label

    >
  13. <div

    class

    =

    "controls"

    >
  14. <input

    id

    =

    "code"

    name

    =

    "code"

    type

    =

    "text"

    placeholder=

    "Enter the Code Above"

    required></

    td

    >
  15. </

    div

    >
  16. </

    div

    >

  17. <div

    class

    =

    "control-group"

    >
  18. <div

    class

    =

    "controls"

    >
  19. <button

    type

    =

    "submit"

    name

    =

    "submit"

    class

    =

    "btn btn-primary"

    ><i

    class

    =

    "icon-ok icon-large"

    ></

    i

    >

    Submit</

    button

    >
  20. </

    div

    >
  21. </

    div

    >

  22. </

    form

    >

Here's the source code for generating a random code.
  1. <

    img src=

    "generatecaptcha.php?rand=<?php echo rand(); ?>"

    id=

    'image_captcha'

    >
  2. <

    a href=

    'javascript: refreshing_Captcha();'

    ><

    i class

    =

    "icon-refresh icon-large"

    ></

    i></

    a>
  3. <

    script language=

    'JavaScript'

    type=

    'text/javascript'

    >
  4. function

    refreshing_Captcha(

    )
  5. {
  6. var

    img =

    document.images

    [

    'image_captcha'

    ]

    ;
  7. img.src

    =

    img.src

    .substring

    (

    0

    ,

    img.src

    .lastIndexOf

    (

    "?"

    )

    )

    +

    "?rand="

    +

    Math

    .random

    (

    )

    *

    1000

    ;
  8. }
  9. </

    script>

In the image below, if the user matches random code.
1_132.png

In the image below, if the user does not match the random code.
2_55.png

Hope that this article will help you a lot.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top