Helper Functions


/ Published in: PHP
Save to your folder(s)

General helper functions I commonly need for new website production


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Helper Functions
  4.  */
  5.  
  6. /**
  7.  * Automated Asset Refresh
  8.  *
  9.  * Returns the full URL of a file, including appending the filemtime onto the URL
  10.  * to break the cache when the file has been updated.
  11.  *
  12.  * @param string Filename in question
  13.  * @return string Filename with base_url appended and the filemtime prepended
  14.  */
  15. function asset_url($filename) {
  16. $url = base_url() . $filename;
  17.  
  18. /**
  19. * Append file modified time to prevent unwanted caching
  20. */
  21. if (file_exists(FCPATH . $filename)) {
  22.  
  23. /**
  24. * Check if there is already a querystring on URL to append appropriate character
  25. */
  26. if (strpos($filename, '?') === FALSE) {
  27. $url .= '?';
  28. } else {
  29. $url .= '&';
  30. }
  31. $url .= filemtime(FCPATH . $filename);
  32. }
  33.  
  34. echo $url;
  35. }
  36.  
  37. /**
  38.  * Generate (Password) Hash
  39.  *
  40.  * Calling generateHash() with a single argument (the plain text password) will cause a
  41.  * random string to be generated and used for the salt. The resulting string consists of
  42.  * the salt followed by the SHA-1 hash - this is to be stored away in your database.
  43.  * When you're checking a user's login, the situation is slightly different in that you
  44.  * already know the salt you'd like to use. The string stored in your database can be
  45.  * passed to generateHash() as the second argument when generating the hash of a user-
  46.  * supplied password for comparison.
  47.  *
  48.  * @param string $plain_text string that we want to hash
  49.  * @param string $salt to compare with the provided $plain_text string
  50.  * @return string resulting has from provided values
  51.  */
  52. function generate_hash ($plain_text, $salt = null) {
  53.  
  54. if ($salt === null) {
  55. $salt = substr(md5(uniqid(rand(), true)), 0, 12);
  56. } else {
  57. $salt = substr($salt, 0, 12);
  58. }
  59.  
  60. return $salt . sha1($salt . $plain_text);
  61.  
  62. }
  63.  
  64. /**
  65.  * Generate Random String
  66.  *
  67.  * Generates a random string of characters
  68.  *
  69.  * @param int $length the length of the random string returned
  70.  * @param string $valid_chars to compare with the provided $plain_text string
  71.  * @return string resulting has from provided values
  72.  */
  73. function random_string( $length = 8, $valid_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ) {
  74. // Starter Variables
  75. $random_string = "";
  76.  
  77. // count the number of chars in the valid chars string so we know how many choices we have
  78. $num_valid_chars = strlen($valid_chars);
  79.  
  80. // repeat the steps until we've created a string of the right length
  81. for ($i = 0; $i < $length; $i++)
  82. {
  83. // pick a random number from 1 up to the number of valid chars
  84. $random_pick = mt_rand(1, $num_valid_chars);
  85.  
  86. // take the random character out of the string of valid chars
  87. // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
  88. $random_char = $valid_chars[$random_pick-1];
  89.  
  90. // add the randomly-chosen char onto the end of our string so far
  91. $random_string .= $random_char;
  92. }
  93.  
  94. // return our finished random string
  95. return $random_string;
  96. }
  97.  
  98. /**
  99.  * General data sanitization
  100.  *
  101.  * Useful for form submissions, this function will take a string or an array of strings
  102.  * and sanitize the data therein by stripping out script/html/style/comment tags. Also
  103.  * strips slashes and does a mysql_real_escape_string
  104.  *
  105.  * @param array|string data to sanitize, can either be a single string or array
  106.  * @return array|string returns a sanitized version of the input
  107.  */
  108. function sanitize($input) {
  109.  
  110. $search = array(
  111. '@<script[^>]*?>.*?</script>@si', // Strip out javascript
  112. '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
  113. '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
  114. '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
  115. );
  116.  
  117. if (is_array($input)) {
  118. foreach($input as $var=>$val) {
  119. $output[$var] = sanitize($val);
  120. }
  121. }
  122. else {
  123. $input = stripslashes($input);
  124. }
  125. $input = preg_replace($search, '', $input);
  126. $output = mysql_real_escape_string($input);
  127. }
  128. return $output;
  129. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.