Lesson 3 - Step 2 - encapsulation


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //a better way...
  2. class MyLogger {
  3.  
  4. private $ipAddr;
  5.  
  6. public function MyLogger(){
  7.  
  8. }
  9.  
  10. public function logDebug($sMsg){
  11. $this->writeLog("DEBUG", $sMsg);
  12. }
  13.  
  14. public function logError($sMsg){
  15. $this->writeLog("ERROR", $sMsg);
  16. }
  17.  
  18. public function logInfo($sMsg){
  19. $this->writeLog("INFO", $sMsg);
  20. }
  21.  
  22. private function writeLog($sSevirity, $sMsg){
  23. error_log("[". $this->getClientIP() ."] :: ". date("j/n/y H:i:s") . $sSevirity .": ". $sMsg);
  24. }
  25.  
  26. private function getClientIP(){
  27. if (!$this->$ipAddr) {
  28. if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
  29. $this->$ipAddr=$_SERVER['HTTP_X_FORWARDED_FOR'];
  30. }
  31. else
  32. if ($_SERVER['HTTP_CLIENT_IP']) {
  33. $this->$ipAddr=$_SERVER['HTTP_CLIENT_IP'];
  34. }
  35. else
  36. $this->$ipAddr= $_SERVER['REMOTE_ADDR'];
  37. }
  38.  
  39. return $this->$ipAddr;
  40. }
  41. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.