PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions.php

https://bitbucket.org/d3bugg3r/shiftsystem
PHP | 71 lines | 50 code | 12 blank | 9 comment | 4 complexity | d2c8abc7fc1de9e42590de6736de2cc7 MD5 | raw file
  1. <?
  2. //this will be included in the main index, making all the defined functions available, and connecting to the mySQL database
  3. $link=mysql_connect ('localhost', 'rota', 'letmein'); // open connection
  4. if (!$link) {
  5. die ("Error, we can't connect to our MySQL server.");
  6. }
  7. MySQL_Select_DB ('rota');
  8. function escape($x) { //using the mysql_real_escape_string without having to contantly retype it during coding
  9. return mysql_real_escape_string($x);
  10. }
  11. function monthly($id, $m, $y) { //Takes a user ID and a month and year and returns the number of hours that user is working in the specified month
  12. $query = "SELECT * FROM hours WHERE worker='$id' AND month='$m' AND year='$y'";
  13. $result = mysql_query($query);
  14. return mysql_num_rows($result);
  15. }
  16. function yearly($id, $y) { //Takes a user ID and a year and returns the number of hours a user is working in that year
  17. $query = "SELECT * FROM hours WHERE worker='$id' AND year='$y'";
  18. $result = mysql_query($query);
  19. return mysql_num_rows($result);
  20. }
  21. function generatePassword ($length = 10)
  22. {
  23. // start with a blank password
  24. $password = "";
  25. // define possible characters - any character in this string can be
  26. // picked for use in the password, so if you want to put vowels back in
  27. // or add special characters such as exclamation marks, this is where
  28. // you should do it
  29. $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
  30. // we refer to the length of $possible a few times, so let's grab it now
  31. $maxlength = strlen($possible);
  32. // check for length overflow and truncate if necessary
  33. if ($length > $maxlength) {
  34. $length = $maxlength;
  35. }
  36. // set up a counter for how many characters are in the password so far
  37. $i = 0;
  38. // add random characters to $password until $length is reached
  39. while ($i < $length) {
  40. // pick a random character from the possible ones
  41. $char = substr($possible, mt_rand(0, $maxlength-1), 1);
  42. // have we already used this character in $password?
  43. if (!strstr($password, $char)) {
  44. // no, so it's OK to add it onto the end of whatever we've already got...
  45. $password .= $char;
  46. // ... and increase the counter by one
  47. $i++;
  48. }
  49. }
  50. // done!
  51. return $password;
  52. }