NET_Helper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Netowrk helper file.
  4. *
  5. * Provides a helper to perform network operations.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package IV
  10. */
  11. require_once(__DIR__ . "/Helper.php");
  12. /**
  13. * Netowrk helper.
  14. *
  15. * Contains netowrk related utilities.
  16. *
  17. * @category Helper.
  18. */
  19. final class NET extends Helper{
  20. /**
  21. * Finds out the protocol the user is connecting to the site with.
  22. *
  23. * @return string "http://" or "https://".
  24. */
  25. public static function get_protocol(){
  26. if (isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on" || $_SERVER["HTTPS"] == 1) || isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] == "https") {
  27. $protocol = "https://";
  28. }
  29. else {
  30. $protocol = "http://";
  31. }
  32. return $protocol;
  33. }
  34. /**
  35. * Finds out the IP address of the client.
  36. *
  37. * @return string Client IP address.
  38. */
  39. public static function get_ip(){
  40. $client = @$_SERVER["HTTP_CLIENT_IP"];
  41. $forward = @$_SERVER["HTTP_X_FORWARDED_FOR"];
  42. $remote = $_SERVER["REMOTE_ADDR"];
  43. if(filter_var($client, FILTER_VALIDATE_IP)){
  44. $ip = $client;
  45. }
  46. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  47. $ip = $forward;
  48. }
  49. else{
  50. $ip = $remote;
  51. }
  52. return $ip;
  53. }
  54. }