Skip to content

Commit 787060d

Browse files
committed
fixed ip getting
1 parent 6875788 commit 787060d

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

cleantalk.class.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ private function createMsg($method, CleantalkRequest $request) {
594594
*/
595595
private function sendRequest($data = null, $url, $server_timeout = 3) {
596596
// Convert to array
597-
$data = json_decode(json_encode($data), true);
597+
$data = (array)json_decode(json_encode($data), true);
598598

599599
// Convert to JSON
600600
$data = json_encode($data);
@@ -697,7 +697,7 @@ private function httpRequest($msg) {
697697
$msg->all_headers=json_encode(apache_request_headers());
698698
//$msg->remote_addr=$_SERVER['REMOTE_ADDR'];
699699
//$msg->sender_info['remote_addr']=$_SERVER['REMOTE_ADDR'];
700-
$si=json_decode($msg->sender_info,true);
700+
$si=(array)json_decode($msg->sender_info,true);
701701
$si['remote_addr']=$_SERVER['REMOTE_ADDR'];
702702
$msg->sender_info=json_encode($si);
703703
if (((isset($this->work_url) && $this->work_url !== '') && ($this->server_changed + $this->server_ttl > time()))
@@ -889,7 +889,7 @@ public function ct_session_ip( $data_ip ) {
889889
if (!$data_ip || !preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $data_ip)) {
890890
return $data_ip;
891891
}
892-
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
892+
/*if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
893893
894894
$forwarded_ip = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
895895
@@ -923,7 +923,8 @@ public function ct_session_ip( $data_ip ) {
923923
}
924924
}
925925
926-
return $data_ip;
926+
return $data_ip;*/
927+
return cleantalk_get_real_ip();
927928
}
928929

929930
/**
@@ -1117,3 +1118,28 @@ function apache_request_headers()
11171118
return( $arh );
11181119
}
11191120
}
1121+
1122+
function cleantalk_get_real_ip()
1123+
{
1124+
if ( function_exists( 'apache_request_headers' ) )
1125+
{
1126+
$headers = apache_request_headers();
1127+
}
1128+
else
1129+
{
1130+
$headers = $_SERVER;
1131+
}
1132+
if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
1133+
{
1134+
$the_ip = $headers['X-Forwarded-For'];
1135+
}
1136+
elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ))
1137+
{
1138+
$the_ip = $headers['HTTP_X_FORWARDED_FOR'];
1139+
}
1140+
else
1141+
{
1142+
$the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
1143+
}
1144+
return $the_ip;
1145+
}

0 commit comments

Comments
 (0)