S
I wrote the code below to show something more "sophisticated", as proposed by the challenge.The first point that caught my attention was the following passage in the AP questionby clicking on this link I could use an echo and display the variable. Now
I am trying to use the friendly URL so technically there is no
plus pagina.php?variavel=valordavariavel.It may not be the case of the AP, but we should be aware that even a URL in the friendly URL format (URL Rewrite), should allow access by normal mode.Many systems mistakenly build their URLs by ignoring the normal way to mount URL parameters. This is a problem because it does not allow the system to be used in environments that do not have a friendly URL feature.This script below, detaches different points from most "solutions" to rescue parameters.1. Support of subfolders and relative URL baseThe "REQUEST_URI" key of the global variable $_SERVER, returns the entire query of a URL. This can be a problem if the script runs within a subfolder. It can also be a problem if the file name is explicitly invoked in the URL.Example: http://localhost/pasta/parametro1/parametro2$_SERVER['REQUEST_URI'] will return /pasta/parametro1/parametro2.If we do not identify the base of the relative URL, we will have a conflict in the parameters.The script below makes this check and also checks the existence of the script file name, for cases of Fake friendly URL.Example: http://localhost/pasta/index.php/parametro1/parametro2$_SERVER['REQUEST_URI'] will return /pasta/index.php/parametro1/parametro2.2. Support normal URL, friendly URL, fake friendly URL and command line (click).In the HTTP protocol, the default and normal format of parameters in a URL is the traditional format using the parameter argumenter.Parameter writer is represented by the question symbol (?).Example: http://localhost/?p=foo/barWe call that "normal URL."In the user-friendly URL, also known as "URL rewrite", "URL rewrite" or "sugar URL", the parameters and the argumenter are omitted.Example: http://localhost/foo/barFor this to work, the environment web server must support URL rewrite. The most popular web server is Apache. In this example, we will only address your use with Apache.The fake friendly URL is a sort of gambiarra that allows you to create friendly URLs without using URL rewrite features like Apache mod_rewrite.This technique arose due to market demand in using friendly URL on hosting servers without support of URL rewrite.Example: http://localhost/index.php/foo/barThe command line, also known as CLI or CLI SAPI, in PHP, is an ignored or poorly supported execution medium by most applications. Typically the most popular market frameworks offer all the features described here, including command-line executions.Example: /caminho/do/compildor/php -f /caminho/do/script/index.php foo/barIn the script below, there is support for command line interface (shell prompt).For the command line, the parameters follow the same format as a friendly URL because the end result of any of the execution media will be the same.3. Support of URL friendly by POST methodWe recommend not mixing different request methods in a single request.Example: <form action="pagina.php?foo=bar" method="POST">
<input type="submit" name="test" value="ok" />
</form>
The "action" parameter of the tag form has parameters in the format GET. However, the defined method is "POST". To rescue this data on pagina.php, it will be necessary to invoke $_GET to get the value of "foo" and $_POST to get the value of "test"Technically there are no serious problems such as data conflict or gender thing. The problem in this is organization and also difficulty in implementing a means of rescuing such data in a flexible and dynamic way.In order to decomplicate, it is recommended that you adopt a standard of sending data by a single method.In the case of sending data by the POST method, a user-friendly URL ceases to have meaning because the parameters are not visible in the URL as in the GET method.Therefore also, it is recommended not to use friendly URL for sending data by POST method.However, we must adapt the demands of the market. Like the use of fake friendly URL, the script below also provides a means to allow URL friendly by the POST method. In this case, the address of the friendly URL is ignored and only the parameters sent by the POST method are extracted.Magic Quotes!It is a native PHP feature that, when enabled, adds escape characters in the values of the received parameters. This feature was discontinued in PHP 5.3.0 and completely removed in version 5.4.0. http://php.net/manual/en/security.magicquotes.php Currently many hosting servers have an environment with PHP 5.3 or even lower versions like 5.1 or 4 with Magic Quotes activated.The script below has this verification as a retrocompatibility feature, allowing you to run in older versions of PHP.<?php
date_default_timezone_set('Asia/Tokyo');
ini_set('error_reporting', E_ALL);
ini_set('log_errors',TRUE);
ini_set('html_errors',FALSE);
ini_set('display_errors',TRUE);
define('CHARSET', 'UTF-8');
ini_set('default_charset', CHARSET);
if (PHP_VERSION < 5.6)
{
ini_set('mbstring.http_output', CHARSET);
ini_set('mbstring.internal_encoding', CHARSET);
}
/**
Caminho base da URL relativa.
Exemplo, caso execute esse script sob um subolder: http://localhost/pasta/index.php
O valor de $path_base deve ser "/pasta/" ($path_base = '/pasta/';)
Caso execute na raíz do endereço, apenas defina com uma barra "/" ($path_base = '/';)
*/
$path_base = '/';
/**
Nome deste arquivo que executa os scripts.
*/
$index_file_name = basename($_SERVER['SCRIPT_FILENAME']);
/**
Verifica se está sendo executado por interface de linha de comando.
Caso a constante PHP_SAPI retorne a string "cli", retorna "true". Caso contrário, "false".
*/
$cli_mode = (strtolower(PHP_SAPI) == 'cli'? true : false);
/**
Caso $cli_mode for falso, resgatamos os parâmetros da chave "REQUEST_URI".
Caso $cli_mode for true, os parâmetros serão resgatados da chave "argv".
*/
$uri = (!$cli_mode? $_SERVER['REQUEST_URI'] : (isset($_SERVER['argv'][1])? $_SERVER['argv'][1] : ''));
/**
Remove o caminho base do parâmetro resgatado de $_SERVER['REQUEST_URI'] ou $_SERVER['argv'].
*/
if (!empty($uri))
{
$len = mb_strlen($path_base.$index_file_name);
if ($path_base.$index_file_name == substr($uri, 0, $len))
$uri = substr($uri, $len);
$len = mb_strlen($path_base);
if ($path_base == substr($uri, 0, $len))
$uri = substr($uri, $len);
}
/**
Caso não esteja sob interface de linha de comando (cli), executa os processos dentro da condicional.
/
if (!$cli_mode)
{
/*
Resgata o método da requisição caso não esteja sob interface de linha de comando (cli).
*/
$method = (isset($_SERVER['REQUEST_METHOD'])? strtoupper($_SERVER['REQUEST_METHOD']) : null);
/**
true: Permite URL amigável pelo método POST.
false: Nega URL amigável pelo método POST.
Recomendável manter como false.
*/
$allow_mode_rewrite_by_post = false;
/**
Verifica se os parâmetros possuem formato normal ou formato de URL reescrita.
*/
if (substr($uri, 0, 1) == '?')
$mode_rewrite = false;
else{
if ($method == 'POST')
{
$mode_rewrite = ($allow_mode_rewrite_by_post? true : false);
}else{
$mode_rewrite = true;
}
}
/**
A query parece provir de uma URL normal.
*/
if (!$mode_rewrite)
{
/**
Nome do parâmetro padrão para recebimento de nomes de rotas.
Exemplo: http://localhost/?p=foo/bar
*/
$route_parameter = 'p';
/**
Nessa condição, resgatamos um parâmetro padrão que contém a rota.
O nome do parâmetro é definido na variável $route_parameter.
*/
$uri = (isset($GLOBALS['_'.$method][$route_parameter]) ? $GLOBALS['_'.$method][$route_parameter] : null);
/**
Suporte para servidores com magic_quotes ativado.
*/
if (!empty($uri) && get_magic_quotes_gpc())
$uri = stripslashes($uri);
}
}
/**
Exemplos de uso
URL normal: http://localhost/index.php?p=foo/bar ou http://localhost/?p=foo/bar
URL amigável falsa: http://localhost/index.php/foo/bar
URL amigável: http://localhost/foo/bar
Linha de comando: /caminho/do/compildor/php -f /caminho/do/script/index.php foo/bar
Para URL amigável sob o módulo Rewrite Module do Apache, o arquivo .htaccess deve conter o seguinte código:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
Options FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.+) /
*/
/**
Configura o ccharset no cabeçalho.
*/
header('Content-Type: text/html; charset='.CHARSET);
/**
Imprime "URI: foo/bar"
O output será sempre a parte relevante da URI.
Exemplo: localhost/foo/bar, localhost/?p=foo/bar e os outros meios, retornam "foo/bar"
*/
echo 'URI: '.$uri;
SEO, HTML link rel="alternate"For SEO purposes, it's important to worry about URL information. If the system allows access to the various URL formats, it is recommended to set link rel="alternate" pointing to the "official" URL. This prevents search engines, such as google bot, from penalizing the page as duplicate of some other previously indexed page.In a practical example, we have the following URL http://localhost/foo/bar that can also be accessed as http://localhost/?p=foo/bar.If the search bot has access to the normal URL, the system must identify that the request did not provide the default URL and add to the HTML tag header <link> whose attribute re be defined as "alternate". For the attribute href, set the default (official) URL where the bot should go in fact and ignore penalties. Example:<link rel="alternate" href="http://localhost/foo/bar" hreflang="en" />