Checking for ZScaler

Last updated 1 month ago

PHP
function check_zscaler_connection() {
    if ( isset( $_SERVER['HTTP_X_ZSCALER_CLIENT_ID'] ) ) {
        echo '<div class="notice notice-info"><p>User is connected via Zscaler.</p></div>';
    } else {
        echo '<div class="notice notice-success"><p>User is not connected via Zscaler.</p></div>';
    }
}
add_action( 'wp_footer', 'check_zscaler_connection' );
PHP
function check_zscaler_ip() {
    // Define Zscaler's IP range (example: a subset of Zscaler's IPs)
    $zscaler_ips = array(
        '185.30.0.0/16', // Example IP range
        // Add more Zscaler IP ranges here
    );

    // Get the visitor's IP address
    $visitor_ip = $_SERVER['REMOTE_ADDR'];

    // Check if the IP address matches Zscaler's IP range
    foreach ( $zscaler_ips as $ip_range ) {
        if ( ip_in_range( $visitor_ip, $ip_range ) ) {
            echo '<div class="notice notice-info"><p>User is connected via Zscaler (IP match).</p></div>';
            return;
        }
    }

    echo '<div class="notice notice-success"><p>User is not connected via Zscaler (IP check).</p></div>';
}

// Helper function to check if an IP is in a given range
function ip_in_range( $ip, $range ) {
    list( $subnet, $mask ) = explode( '/', $range );
    $subnet = ip2long( $subnet );
    $mask = 0xFFFFFFFF << (32 - $mask);
    return ( ip2long( $ip ) & $mask ) === ( $subnet & $mask );
}

add_action( 'wp_footer', 'check_zscaler_ip' );
PHP
function check_zscaler_reverse_dns() {
    $visitor_ip = $_SERVER['REMOTE_ADDR'];
    $reverse_dns = gethostbyaddr( $visitor_ip );

    if ( strpos( $reverse_dns, 'zscaler.net' ) !== false ) {
        echo '<div class="notice notice-info"><p>User is connected via Zscaler (Reverse DNS match).</p></div>';
    } else {
        echo '<div class="notice notice-success"><p>User is not connected via Zscaler (Reverse DNS check).</p></div>';
    }
}
add_action( 'wp_footer', 'check_zscaler_reverse_dns' );

All code snippets are licensed GPLv2 or later unless otherwise stated.