client.php ➔ GetClientIp()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * Funcs about client side info
4
 *
5
 * @package		fwolflib
6
 * @subpackage	func
7
 * @copyright	Copyright 2003-2009, Fwolf
8
 * @author		Fwolf <[email protected]>
9
 * @since		2006-07-03
10
 */
11
12
13
require_once(dirname(__FILE__) . '/../fwolflib.php');
14
15
16
/**
17
 * Convert user ip from hex string
18
 *
19
 * @deprecated      Use Fwlib\Util\Ip::fromHex()
20
 * @param	string	$hex
21
 * @return	string
22
 * @link	http://us.php.net/uniqid
23
 */
24 View Code Duplication
function ClientIpFromHex($hex) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
	$ip = "";
26
	if (8 == strlen($hex)) {
27
		$ip .= hexdec(substr($hex ,0 ,2)) . '.';
28
		$ip .= hexdec(substr($hex ,2 ,2)) . '.';
29
		$ip .= hexdec(substr($hex ,4 ,2)) . '.';
30
		$ip .= hexdec(substr($hex ,6 ,2));
31
	}
32
    return $ip;
33
} // end of func ClientIpFromHex
34
35
36
/**
37
 * Convert user ip to hex string
38
 *
39
 * @deprecated      Use Fwlib\Util\Ip::toHex()
40
 * @param	string	$ip
41
 * @return	string
42
 * @link	http://us.php.net/uniqid
43
 */
44
function ClientIpToHex($ip = "") {
45
	$hex = "";
46
	if('' == $ip)
47
		//$ip = getenv('REMOTE_ADDR');
48
		$ip = GetClientIp();
0 ignored issues
show
Deprecated Code introduced by
The function GetClientIp() has been deprecated with message: Use Fwlib\Util\HttpUtil::getClientIp()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
49 View Code Duplication
	if (false == ip2long($ip))
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ip2long($ip) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
		return '';
51
	else {
52
		$part = explode('.', $ip);
53
		if (4 != count($part))
54
			return '';
55
		else
56
			for ($i=0; $i<=count($part)-1; $i++) {
57
				$hex .= substr('0' . dechex($part[$i]), -2);
58
			}
59
	}
60
	return $hex;
61
} // end of func ClientIpToHex
62
63
64
/**
65
 * 检查客户端的浏览器是NS还是IE
66
 *
67
 * @deprecated      Use Fwlib\Util\HttpUtil::getBrowserType()
68
 * @return	string
69
 */
70
function GetBrowserType()
71
{
72
	$str = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
73
	if (false === strpos($str, 'MSIE')) {
74
	    return('NS');
75
	}
76
	else {
77
	    return('IE');
78
	}
79
} // end func GetBrowserType
80
81
82
/**
83
 * Get ip of client
84
 *
85
 * @deprecated      Use Fwlib\Util\HttpUtil::getClientIp()
86
 * @return	string
87
 * @link http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html
88
 */
89
function GetClientIp() {
90
	$s = '';
0 ignored issues
show
Unused Code introduced by
$s is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
92
	// Original way: check ip from share internet
93
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
94
		$s = $_SERVER['HTTP_CLIENT_IP'];
95
	}
96
	// Using proxy ?
97
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
98
		$s = $_SERVER['HTTP_X_FORWARDED_FOR'];
99
	}
100
	// Another way
101
	elseif (!empty($_SERVER['REMOTE_ADDR'])) {
102
		$s = $_SERVER['REMOTE_ADDR'];
103
	}
104
	else {
105
		$s = '';
106
	}
107
	return $s;
108
} // end of func GetClientIp
109
110
111
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
112