You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.6 KiB

  1. <?php
  2. /**
  3. * Silahkan buat fungsi helper di dalam file ini dengan format seperti
  4. * contoh di bawah. Isi keterangan parameter serta tipe data hasil return
  5. * nya.
  6. */
  7. require_once('_db.php');
  8. require_once('_collection.php');
  9. /**
  10. * CONTOH
  11. * Mengembalikan nilai $a
  12. *
  13. * @param Int $a
  14. * @return Int
  15. */
  16. function helper(Int $a) {
  17. return $a;
  18. }
  19. /**
  20. * Mendapatkan IP user
  21. *
  22. * @return String
  23. */
  24. function getRealIpAddr() {
  25. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  26. //check ip from share internet
  27. $ip = $_SERVER['HTTP_CLIENT_IP'];
  28. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  29. //to check ip is pass from proxy
  30. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  31. } else {
  32. $ip = $_SERVER['REMOTE_ADDR'];
  33. }
  34. return $ip;
  35. }
  36. /**
  37. * Dumping data, tak bentuk jadi JSON
  38. *
  39. * @return Any
  40. */
  41. function dump($data) {
  42. header('Content-Type: application/json');
  43. echo json_encode($data);
  44. die();
  45. }
  46. /**
  47. * Fungsi hash_equals() hanya terdapat di PHP >= 5.6. Sehingga harus buat fungsi
  48. * hash_equal manual untuk PHP < 5.
  49. */
  50. if (!function_exists('hash_equals')) {
  51. /**
  52. * Membandingan 2 hash.
  53. *
  54. * @param String $str1
  55. * @param String $str2
  56. * @return Boolean
  57. */
  58. function hash_equals($str1, $str2) {
  59. if (strlen($str1) != strlen($str2)) {
  60. return false;
  61. } else {
  62. $res = $str1 ^ $str2;
  63. $ret = 0;
  64. for ($i = strlen($res) - 1; $i >= 0; $i--) {
  65. $ret |= ord($res[$i]);
  66. }
  67. return !$ret;
  68. }
  69. }
  70. }