As the PHP Manual says:
$_GET
is an associative array of variables passed to the current script
via URL parameters.
For example in a URL like this:
http://www.dominio.com/archivo.php?id=1&nombre=Pedro
The value of $_GET
will be an array like this:
array('id'=>1, 'nombre'=>'Pedro');
If you want to get the values of GET
:
$_GET["id"]; //obtienes 1
$_GET["nombre"]; //obtienes Pedro
To solve your problem
Seeing that your URL is built like this:
#Regla Url amigable
RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?id=$1
It would be enough to change in the rule the value id
per url
#Regla Url amigable
RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?url=$1
If, on the contrary, the value url
were taken from another element, or if it equals some file through which you build friendly URLs, then you would have to modify the name of the element (for example an HTML element) or the File name if it was a file.
Other things about GET
I guess you're going to use GET
or POST
more than once, that's why I allow myself to leave some indications ...
When you submit HTML forms, GET will associate the values using as key the value of the name
tag of the elements.
Example:
<form>
<input type="number" name="id" />
<input type="text" name="nombre" />
</form>
If you want to match the keys you will get via GET with the names of the columns in your database, you only have to change the value of the name
tag if you use an HTML form, or the values of the URL.
Example:
HTML form
<form action="archivo.php" method="GET">
<input type="number" name="id_producto" placeholder="Introduzca id_producto"/>
<input type="number" name="id_usuario" placeholder="Introduzca id_usuario"/>
<input type="text" name="url" placeholder="Introduzca url"/>
</form>
When you submit this form, what actually occurs is a URL like this:
http://www.dominio.com/archivo.php?id_usuario=1&id_producto=24890&url=example.com/registrar
Then, with GET
we get an array with three keys and its three respective values, each pair (key-> value) is separated by &
in the URL:
-
id_usuario => 1
-
id_producto => 24890
-
url => example.com/registrar
file.php or file that receives data from the previous form
In the PHP file you would recover each value like this:
$_GET["id_producto"];
$_GET["id_usuario"];
$_GET["url"];
PHP manual notes and experience
Some indications regarding the use of GET
(which are also valid for POST
):
- When the values come from an HTML document, GET collects the values using the
name
tag of the HTML elements, not the id
tag.
-
Usually isset
is used to verify that the searched value exists in the URL (if it exists between the posted data). For example, to find out if id_usuario
was posted:
if(isset($_GET['id_usuario'])) {
//existe
}else{
//no existe
}
This comparison can be refined, looking not only if it exists, but also if it is not blank.
-
This is a 'superglobal' or a global automatic variable. It simply means that it is a variable that is available anywhere in the script. You do not need to do global $variable;
to access it from functions or methods.
-
GET variables are passed via urldecode()
.
-
$_POST
works the same way and is sometimes recommended with respect to GET
, but that would be another question.
-
Never send directly retrieved values with GET
or with POST
or any other value coming from outside to database queries . Doing that would make the code vulnerable to SQL Injection, through which malicious users could take control not only of the database, but of your entire system. These values should be sent through prepared queries. Here in SO there are several questions that deal with this issue.