Picking URL parameters in PHP

Asked

Viewed 1,205 times

2

Address http://exemplo/registrar.php

For example, I would like to perform a record by registrar.php passing the data through bars.

Example http://exemplo/registrar/nomedapessoa

How could I do this in PHP?

1 answer

4

You can use .htaccess chance is using Apache.

Create a file called .htaccess in the main folder of your website 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 (it would be the register.php):

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

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

echo '<pre>';

print_r($paths);

echo '</pre>';

So just access http://exemplo/registrar/nomedapessoa that will be shown the content of http://exemplo/registrar.php/nomedapessoa

Browser other questions tagged

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