Skip to content

Instantly share code, notes, and snippets.

@maugelves
Created December 8, 2016 14:14
Show Gist options
  • Save maugelves/691a0064af04bf0ff7dcf7f3bd55b594 to your computer and use it in GitHub Desktop.
Save maugelves/691a0064af04bf0ff7dcf7f3bd55b594 to your computer and use it in GitHub Desktop.
Función que devuelve la URL de una petición HTTP a partir de las variables de $_SERVER.
<?php
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function full_url( $s, $use_forwarded_host = false )
{
return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}
/* =========================
Ejemplo de uso
$direccion_url = full_url( $_SERVER );
echo direccion_url;
================================ */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment