PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/user_guide_src/source/helpers/captcha_helper.rst

https://github.com/dchill42/CodeIgniter
ReStructuredText | 143 lines | 109 code | 34 blank | 0 comment | 0 complexity | becfa297696adb0a2b87cf6bc549e743 MD5 | raw file
  1. ##############
  2. CAPTCHA Helper
  3. ##############
  4. The CAPTCHA Helper file contains functions that assist in creating
  5. CAPTCHA images.
  6. .. contents:: Page Contents
  7. Loading this Helper
  8. ===================
  9. This helper is loaded using the following code
  10. ::
  11. $this->load->helper('captcha');
  12. The following functions are available:
  13. create_captcha()
  14. ================
  15. .. php:function:: function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  16. :param array $data: Array of data for the CAPTCHA
  17. :param string $img_path: Path to create the image in
  18. :param string $img_url: URL to the CAPTCHA image folder
  19. :param string $font_path: Server path to font
  20. :returns: array('word' => $word, 'time' => $now, 'image' => $img)
  21. Takes an array of information to generate the CAPTCHA as input and
  22. creates the image to your specifications, returning an array of
  23. associative data about the image.
  24. ::
  25. array(
  26. 'image' => IMAGE TAG
  27. 'time' => TIMESTAMP (in microtime)
  28. 'word' => CAPTCHA WORD
  29. )
  30. The **image** is the actual image tag::
  31. <img src="http://example.com/captcha/12345.jpg" width="140" height="50" />
  32. The **time** is the micro timestamp used as the image name without the
  33. file extension. It will be a number like this: 1139612155.3422
  34. The **word** is the word that appears in the captcha image, which if not
  35. supplied to the function, will be a random string.
  36. Using the CAPTCHA helper
  37. ------------------------
  38. Once loaded you can generate a captcha like this::
  39. $vals = array(
  40. 'word' => 'Random word',
  41. 'img_path' => './captcha/',
  42. 'img_url' => 'http://example.com/captcha/',
  43. 'font_path' => './path/to/fonts/texb.ttf',
  44. 'img_width' => '150',
  45. 'img_height' => 30,
  46. 'expiration' => 7200
  47. );
  48. $cap = create_captcha($vals);
  49. echo $cap['image'];
  50. - The captcha function requires the GD image library.
  51. - Only the **img_path** and **img_url** are required.
  52. - If a **word** is not supplied, the function will generate a random
  53. ASCII string. You might put together your own word library that you
  54. can draw randomly from.
  55. - If you do not specify a path to a TRUE TYPE font, the native ugly GD
  56. font will be used.
  57. - The "captcha" folder must be writable (666, or 777)
  58. - The **expiration** (in seconds) signifies how long an image will remain
  59. in the captcha folder before it will be deleted. The default is two
  60. hours.
  61. Adding a Database
  62. -----------------
  63. In order for the captcha function to prevent someone from submitting,
  64. you will need to add the information returned from ``create_captcha()``
  65. to your database. Then, when the data from the form is submitted by
  66. the user you will need to verify that the data exists in the database
  67. and has not expired.
  68. Here is a table prototype::
  69. CREATE TABLE captcha (  
  70. captcha_id bigint(13) unsigned NOT NULL auto_increment,  
  71. captcha_time int(10) unsigned NOT NULL,  
  72. ip_address varchar(45) NOT NULL,  
  73. word varchar(20) NOT NULL,  
  74. PRIMARY KEY `captcha_id` (`captcha_id`),  
  75. KEY `word` (`word`)
  76. );
  77. Here is an example of usage with a database. On the page where the
  78. CAPTCHA will be shown you'll have something like this::
  79. $this->load->helper('captcha');
  80. $vals = array(     
  81. 'img_path' => './captcha/',     
  82. 'img_url' => 'http://example.com/captcha/'     
  83. );
  84. $cap = create_captcha($vals);
  85. $data = array(     
  86. 'captcha_time' => $cap['time'],     
  87. 'ip_address' => $this->input->ip_address(),     
  88. 'word' => $cap['word']     
  89. );
  90. $query = $this->db->insert_string('captcha', $data);
  91. $this->db->query($query);
  92. echo 'Submit the word you see below:';
  93. echo $cap['image'];
  94. echo '<input type="text" name="captcha" value="" />';
  95. Then, on the page that accepts the submission you'll have something like
  96. this::
  97. // First, delete old captchas
  98. $expiration = time() - 7200; // Two hour limit
  99. $this->db->where('captcha_time < ', $expiration)
  100. ->delete('captcha');
  101. // Then see if a captcha exists:
  102. $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
  103. $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
  104. $query = $this->db->query($sql, $binds);
  105. $row = $query->row();
  106. if ($row->count == 0)
  107. {     
  108. echo 'You must submit the word that appears in the image.';
  109. }