您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

utils.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. function starts_with($haystack, $needle)
  3. {
  4. return ($needle === ""
  5. || strrpos($haystack, $needle, -strlen($haystack)) !== false);
  6. }
  7. function ends_with($haystack, $needle)
  8. {
  9. return ($needle === ""
  10. || strpos($haystack, $needle, strlen($haystack) - strlen($needle))
  11. !== false);
  12. }
  13. function error($code, $message)
  14. {
  15. header("HTTP/ $code $message");
  16. die(json_encode(array("code" => intval($code), "message" => $message)));
  17. }
  18. function get_param($array, $key, $is_error)
  19. {
  20. if (isset($array[$key]))
  21. return $array[$key];
  22. else if ($is_error)
  23. error(400, "Missing parameters");
  24. else
  25. return false;
  26. }
  27. function get_post($key, $is_error = true)
  28. {
  29. return get_param($_POST, $key, $is_error);
  30. }
  31. function get_get($key, $is_error = true)
  32. {
  33. return get_param($_GET, $key, $is_error);
  34. }
  35. function check_float($f, $def = null)
  36. {
  37. return ($f == (string)(float)$f) ? $f : $def;
  38. }
  39. function check_int($i, $def = null)
  40. {
  41. return is_numeric($i) ? $i : $def;
  42. }
  43. function check_table_field($header, $table, $field)
  44. {
  45. $headers = apache_request_headers();
  46. if (!isset($headers[$header]))
  47. return false;
  48. $u = database_exec("SELECT id FROM $table WHERE `$field` = :data",
  49. array(":data" => $headers[$header]))->fetch();
  50. if (!$u)
  51. return false;
  52. return $headers[$header];
  53. }
  54. ?>