Récupérer le nom d'une page courante https

Par Frank Hélin, il y a 11 ans


Bonjour, J'ai lu sur un forum que le code qui suit ne fonctionne pas pour les sites sécurisés https, si c'est vrai, comment faire autrement ?

$monUrl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

Merci !

7 réponses

Frank Hélin, il y a 11 ans

Bonjour, je manque de connaissances pour alimenter une réflexion, d'où ma présence sur ce forum et mes recherches sur le net....malheureusement infructueuses jusqu'à maintenant.

iriven, il y a 11 ans

salut je te propose ceci:

class siteURL { private static function scheme() { $Scheme = 'http'; $Scheme .=(self::isSecure() === true) ? 's://':'://' ; return $Scheme; } private static function isSecure() { return (bool)(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) ? true:false ; } private static function httpPort() { $port = $_SERVER['SERVER_PORT']; $isSecure = self::isSecure(); return (($port != 80 and !$isSecure) or ($port != 443 and $isSecure))? (int)$port : '' ; } public static function __toString() { $url = self::scheme().self::hostname().((self::httpPort() !== '' )? ':'.self::httpPort() : ''); return rtrim($url, '/\\'); } private static function hostname() { $keys = array('HTTP_HOST','SERVER_NAME'); foreach ($keys as $key) { if(!array_key_exists($key,$_SERVER)) continue; $host = $_SERVER[$key]; if(preg_match('/(localhost|127\.0\.0\.1)/', $host) || $_SERVER['SERVER_ADDR'] == '127.0.0.1') return null; // prevent problems on local setups $host = strtolower( htmlentities(strip_tags((string) $host))); return preg_replace('/(^www\.|:\d+$)/i', NULL, $host); } return null; } }

Utilisation:

$baseUrl= siteUrl::__toString(); echo $baseUrl;
Frank Hélin, il y a 11 ans

Un peu complexe, mais je vais tâcher de comprendre. Merci !

Frank Hélin, il y a 11 ans

Brandon, en faisant une recherche sur Google, je suis tombé sur le forum Grafikart. Jolie mise en abime. ;-)

betaWeb, il y a 11 ans

@iriven: Sympa cette petite classe :)
@Frank Hélin: Tu appelles ça "complexe" ? C'est simplissime ;)

Frank Hélin, il y a 11 ans

Je trouverai ça simple quand jaurai compris :-)

iriven, il y a 11 ans

dommage que tu trouve çà compliqué. parce que je ne vois pas comment tu peux contruire une url qui restes valide tant en http qu'en https sans passer par un systeme pareil. puisque d'apres mon post precedent, pour recuperer ton url, tu devras simplement poser ceci:

$monUrl = $baseUrl.'/'.ltrim($_SERVER['REQUEST_URI'],'/');

meme ci dans la realité, pour un vrai projet recuperer l'uri courant en detectant simplement la variable $_SERVER['REQUEST_URI']
est insuffisant