Absolute address (including script) in PHP

Asked

Viewed 42 times

0

Consider the following html address:

http://exemplo.com/1/2/3/teste.php

I would like to indicate in the variable $ender the absolute address of my scripts (including them) to be found independent of the change of address (from being at the root or subdirectory). I tried that "http://$_SERVER[HTTP_HOST]$SERVER[REQUEST_URI]" to try to at least replace that: (http://exemplo.com/1/2/3/). It didn’t work. What wanted to avoid the literality of everything (http://exemplo.com/1/2/3/teste.php). I only had some partial success with http://$_SERVER[HTTP_HOST], but only replaces the base address.

<?php
error_reporting(E_ALL);
$host = $_SERVER['HTTP_HOST']$SERVER['REQUEST_URI']/;
$id   = $_GET['id'];
if ($id == 1) {
  • You’re trying to interpolate that right into a string, that’s it? $_SERVER['REQUEST_URI'] brings everything that comes after the (host) domain, that’s not what you need?

  • I tried this but it didn’t work (if the way I described above is correct). tb didn’t get it with $_SERVER[DOCUMENT_ROOT]

  • I just can’t figure out where the problem is. Show a piece of your real code, the way it’s in the question we don’t quite know what you’re doing

  • @bfavaretto, includes the code part of how I put the address.

1 answer

0

It seems you have a syntax error there. Try it like this:

$protocolo = empty($_SERVER['HTTPS']) ? 'http://': 'https://';
$url = $protocolo . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $url;
  • It did not work to the satisfaction. Even if only this part is considered: http://exemplo.com/1/2/3/ only partially resolves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.