How to get current url with PHP

0

I have this link below.

http://example.com/Novo/adm/categoria/1-Nome_Categoria

http://example.com/Novo/adm.php?categoria=1

But I'll need to compare current URLs to make a menu activation scheme.

Below is the code that displays this result above.

<?php 
    $URL_ATUAL= "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    echo $URL_ATUAL;
?>

For needing to make this comparison I just need to get the URL below.

adm/categoria/1-Nome_Categoria

I tried to use the explode but did not return the correct URL.

    
asked by anonymous 25.04.2017 / 16:51

3 answers

2

As stated in the comments, the content of the global variable $_SERVER["REQUEST_URI"] is of the form:

Novo/adm/categoria/1-Nome_Categoria

It was also expressed in the comments that the content of interest is just the part of adm on, so just treat this value by removing what is not of interest. This can be done in many ways.

If the directory name does not change over time, just removing the contents of the string is enough:

$url = str_replace("Novo/", "", $_SERVER["REQUEST_URI"]);

In this way, $url would be adm/categoria/1-Nome_Categoria , as desired.

If the directory name is changeable over time, but there is always a directory, you can only remove the first part of the string until the first occurrence of / :

$url = substr($_SERVER["REQUEST_URI"], strpos($_SERVER["REQUEST_URI"], '/')+1);

The result will be the same regardless of the directory name, as long as there is one.

If there is a possibility that there is more than one directory, but that the URL will always start with adm , you can search for it in your URL, like this:

$url = substr($_SERVER["REQUEST_URI"], strpos($_SERVER["REQUEST_URI"], 'adm'));

The result would also be the same regardless of the number and name of the directories present in the URL.

    
25.04.2017 / 17:07
1

You can use

<?php
echo $_SERVER['SCRIPT_URI'];
?>

or the short form

<?=$_SERVER['SCRIPT_URI']?>
    
26.09.2018 / 20:20
-1

I have a very nice class for this task, feel free to use it, give an opinion, study, etc.

This would be the way to use it:

<?php $uri = URI::base(); echo $uri; ?>

or just ...

<?php echo URI::base(); ?>

link

Below the class structure

<?php
class URI {

/**
 * $protocolo
 * @var string | $protocolo
 * @access private
 */
static private $protocolo;
/**
 * $host
 * @var string | $host
 * @access private
 */
static private $host;
/**
 * $scriptName
 * @var string | $scriptName
 * @access private
 */
static private $scriptName;
/**
 * $finalBase
 * @var string | $finalBase
 * @access private
 */
static private $finalBase;

/**
 * protected function Protocolo()
 * ----------------------------------------------
 *            Obtém o protocolo da url
 * ----------------------------------------------
 * @return string | Ex: http://... - https://...
 * @access protected
 */
protected function Protocolo()
{
    /**
     * Faz a verificação se for
     * diferente de https
     */
    if(strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false)
    {
        self::$protocolo = 'http://'; //Atribui o valor http
    }
    else
    {
        self::$protocolo = 'https://'; //Atribui o valor https
    }
    /**
     * Retorna o protocolo em formato string
     * @var string
     */
    return self::$protocolo;
}
/**
 * protected function Host()
 * ----------------------------------------------
 *            Obtém o host principal
 * ----------------------------------------------
 * @return string | Ex: www.example.com.br
 * @access protected
 */
protected function Host()
{
    self::$host = $_SERVER['HTTP_HOST']; //Atribui o valor www.example.com.br
    /**
     * Retorna o host em formato string
     * @var string
     */
    return self::$host;
}
/**
 * protected function scriptName()
 * ----------------------------------------------
 * Obtém o script name do host após a primeira barra
 * ----------------------------------------------
 * @return string | Ex: .../dir/index.php
 * @access protected
 */
protected function scriptName()
{
    /**
     * $scr
     * Atribui o valor do SCRIPT_NAME em uma
     * variável $scr e utiliza-se a função dirname()
     * para remover qualquer nome de arquivo .html, .php, etc...
     * @var string
     */
    $scr = dirname($_SERVER['SCRIPT_NAME']);
    /**
     * Faz a contagem de barras que contém a url principal
     * o objetivo aqui é pegar o nível de pasta onde hospeda-se o diretório
     * caso ele exista.
     */
    if(!empty($scr) || substr_count($scr, '/') > 1)
    {
        self::$scriptName = $scr . '/'; //atribui o valor do diretório com uma "/" na sequência
    }
    else
    {
        self::$scriptName = ''; //atribui um valor vazio
    }
    /**
     * Retorna o scriptName em formato string
     * @var string
     */
    return self::$scriptName;
}
/**
 * public function base()
 * ----------------------------------------------
 *          Monta a url base e retorna
 * ----------------------------------------------
 * @return [type] [description]
 * @access public
 */
public function base()
{
    //Concatena os valores
    self::$finalBase = self::Protocolo() . self::Host() . self::scriptName();
    /**
     * Retorna toda a url construida em formato string
     * @var string
     */
    return self::$finalBase;
}
}
?>
    
15.08.2017 / 19:06