Skip to content

Instantly share code, notes, and snippets.

@alexandermitsyk
Last active August 23, 2019 02:16
Show Gist options
  • Save alexandermitsyk/67e9d2319d247ea635a90db9a1d0d428 to your computer and use it in GitHub Desktop.
Save alexandermitsyk/67e9d2319d247ea635a90db9a1d0d428 to your computer and use it in GitHub Desktop.
AWS Cloudfront Header Checker For PHP
<?php
function getHeaders( $header_name = null ) {
$headervals = "";
$keys = array_keys( $_SERVER );
if ( is_null( $header_name ) ) {
$headers = preg_grep( "/^HTTP_(.*)/si", $keys );
} else {
$header_name_safe = str_replace( "-", "_", strtoupper( preg_quote( $header_name ) ) );
$headers = preg_grep( "/^HTTP_${header_name_safe}$/si", $keys );
}
foreach ( $headers as $header ) {
if ( is_null( $header_name ) ) {
$headervals[ substr( $header, 5 ) ] = $_SERVER[ $header ];
} else {
return $_SERVER[ $header ];
}
}
return $headervals;
}
$currenthost = $_SERVER[ 'HTTP_HOST' ];
$CloudFront_Viewer_Country = getHeaders( "CloudFront-Viewer-Country" );
$X_Forwarded_For_Cloudfront = getHeaders( "X-Forwarded-For" );
$CloudFront_Viewer_RemoteA = $_SERVER[ 'REMOTE_ADDR' ];
$X_USER_REAL_IP = explode( ",", $X_Forwarded_For_Cloudfront );
$getLanguage = getHeaders( "Accept-Language" );
$explode = explode( ";", $getLanguage );
$GetRealLanguage = explode( ",", $explode[ 0 ] );
$CloudFront_Accept_Language = $GetRealLanguage[ 0 ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment