Skip to content

Commit 32888cf

Browse files
committed
Added version 1.0.0
1 parent aac50d3 commit 32888cf

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

core/cvwp-helpers-function.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Helpers
4+
*
5+
* @package Count Visitors WP
6+
* @since 1.0.0
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit;
11+
}
12+
13+
/**
14+
* Get visitor original IP address.
15+
*
16+
* @since 1.0.0
17+
*
18+
* @param bool $validate True if wants some validations.
19+
* @return string String formatted IP address of user.
20+
*/
21+
function count_visitors_get_ip( $validate = true ) {
22+
// ipkeys.
23+
$ipkeys = array(
24+
'REMOTE_ADDR',
25+
'HTTP_CLIENT_IP',
26+
'HTTP_X_FORWARDED_FOR',
27+
'HTTP_X_FORWARDED',
28+
'HTTP_FORWARDED_FOR',
29+
'HTTP_FORWARDED',
30+
'HTTP_X_CLUSTER_CLIENT_IP',
31+
);
32+
33+
$ip = array();
34+
foreach ( $ipkeys as $keyword ) {
35+
if ( ! empty( $_SERVER[ $keyword ] ) ) {
36+
$server_keyword = sanitize_text_field( wp_unslash( $_SERVER[ $keyword ] ) );
37+
if ( $validate ) {
38+
if ( count_visitors_validate_ip( $server_keyword ) ) {
39+
$ip[] = $server_keyword;
40+
}
41+
} else {
42+
$ip[] = $server_keyword;
43+
}
44+
}
45+
}
46+
47+
$ip = ( empty( $ip ) ? 'Unknown' : implode( ', ', $ip ) );
48+
return $ip;
49+
}
50+
51+
/**
52+
* Validate IP address.
53+
*
54+
* @since 1.0.0
55+
*
56+
* @param string $ip String IP address of user.
57+
* @return bool Return status validity of IP address.
58+
*/
59+
function count_visitors_validate_ip( $ip ) {
60+
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) {
61+
return true;
62+
} else {
63+
return false;
64+
}
65+
}

0 commit comments

Comments
 (0)