Skip to content

Commit

Permalink
Merge pull request #333 from tianyong90/master
Browse files Browse the repository at this point in the history
Add a helper to get correct client ip address. fixed #316
  • Loading branch information
overtrue committed Mar 14, 2016
2 parents 394d983 + 0b1eb33 commit 7425275
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Payment/LuckyMoney/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function send(array $params, $type = self::TYPE_NORMAL)
public function sendNormal($params)
{
$params['total_num'] = 1;
$params['client_ip'] = !empty($params['client_ip']) ? $params['client_ip'] : $_SERVER['HTTP_CLIENT_IP'];
$params['client_ip'] = !empty($params['client_ip']) ? $params['client_ip'] : \EasyWeChat\Payment\get_client_ip();

return $this->send($params, self::TYPE_NORMAL);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Payment/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,30 @@ function generate_sign(array $attributes, $key, $encryptMethod = 'md5')

return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
}

/**
* Get client ip address.
*
* @return string
*/
function get_client_ip()
{
$ipAddress = '';
if (getenv('HTTP_CLIENT_IP')) {
$ipAddress = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ipAddress = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_X_FORWARDED')) {
$ipAddress = getenv('HTTP_X_FORWARDED');
} elseif (getenv('HTTP_FORWARDED_FOR')) {
$ipAddress = getenv('HTTP_FORWARDED_FOR');
} elseif (getenv('HTTP_FORWARDED')) {
$ipAddress = getenv('HTTP_FORWARDED');
} elseif (getenv('REMOTE_ADDR')) {
$ipAddress = getenv('REMOTE_ADDR');
} else {
$ipAddress = 'UNKNOWN';
}

return $ipAddress;
}

0 comments on commit 7425275

Please sign in to comment.