Get URL parameters in PHP

2

Address http://exemplo/registrar.php

For example, I would like to register by registrar.php passing data through bars.

Example http://exemplo/registrar/nomedapessoa

How could I do this in PHP?

    
asked by anonymous 20.06.2015 / 20:21

1 answer

4

You can use .htaccess to be using Apache.

Create a file named .htaccess in the main folder of your site and add the following content:

RewriteEngine On
RewriteBase /

#Verifica se o arquivo existir então ignora a reescrita
RewriteCond %{REQUEST_FILENAME} !-f

#Verifica se a pasta existir então ignora a reescrita
RewriteCond %{REQUEST_FILENAME} !-d

#Reescreve a URL para acessar arquivos PHP e o PATH_INFO
RewriteRule ^(.*)/(.*)$ $1.php/$2 [L,QSA]

The php code for testing should look something like (would register.php):

<?php
echo 'Path: ', $_SERVER['PATH_INFO'], '<br>';

//Extraindo PATH_INFO
$paths = explode('/', $_SERVER['PATH_INFO']);

echo '<pre>';

print_r($paths);

echo '</pre>';

Then just access http://exemplo/registrar/nomedapessoa which will show the contents of http://exemplo/registrar.php/nomedapessoa

    
20.06.2015 / 20:28