Functions in PHP

December 4th, 2005 by Quatrux

custom Functions in PHP is one of the best things available to optimize your code, the things you can do with it are enormous, here is my two examples of some functions which you can use.

  1. < ?php
  2.  
  3. function pw_encode($pass) {
  4.         /* Check Input */
  5.         if (is_string($pass) AND !empty($pass)) {
  6.                 /* Make a Random Seed */        $s = '';
  7.                 for ($i = 0; $i < 8; $i++) {
  8.                         $s .= substr('0123456789abcdef', mt_rand(0,15), 1);
  9.                 }
  10.                 return md5($s.$pass).$s;
  11.         } else {
  12.                 user_error('pw_encode() The input should be non empty string', E_USER_WARNING);
  13.                 return FALSE;
  14.         }
  15. }
  16.  
  17. function pw_check($pass, $value) {
  18.         /* Check Input */
  19.         if (is_string($pass) AND is_string($value) AND !empty($pass) AND !empty($value)) {
  20.                 /* Get the Seed */
  21.                 $s = substr($value, 32, 8 );
  22.                 /* Check the Passwords */
  23.                 if (md5($s.$pass).$s == $value) {
  24.                         return TRUE;
  25.                 } else {
  26.                         return FALSE;
  27.                 }
  28.         } else {
  29.                 user_error('pw_decode() The both input values should be non empty strings', E_USER_WARNING);
  30.                 return FALSE;
  31.         }
  32. }

so now, how would you use those functions ? it is really simple, if you know some PHP, here is an example, you just need to call the function like an ordinary compiled php function :D

  1. < ?php
  2. /* Encode the Password */
  3. $password = pw_encode(‘my_pass’);
  4. // it is recommended to store the password somewhere in your database
  5. /* Check the Password */
  6. // the password could come from a Post method and the $password from the database or a txt file etc.
  7. if (pw_check($password, ‘my pass’)) {
  8.         echo ‘the password is correct’;
  9. } else {
  10.         echo ‘you forgot your password ?’;
  11. }
  12. ?>

This is just one example, in functions you can’t use variables from outside the script, unless you make them global, or you can use super globals like $_SERVER, $_GET, $_POST, $_SESSION inside the functions also $GLOBALS[] array and constants which you defined using define() like if you would like to use mysql_resource in the function, just pass it through into the function: function name($a, $b, $mysql_connection) {} and whola you can use mysql in the function without needing to connect inside the function, same for ftp and irc gateway connections. Don’t forget that include() function works well in the functions or you can use file() and everything you want, as I said it is enormous thing, imagine your simple site: file index.php

  1. < ?php
  2.  
  3. /* My Site*/
  4. include "functions.php";
  5. echo_site();
  6. ?>

and the function will just output everything, well it is not the best way but it is possible. ;)
Enough for today :D

Posted in PHP, Webmastering | No Comments »

Leave a Comment



Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment and don't be afraid of the spam protection!