What value does a $ _GET give you?

5

I have the following code where I get data from the Urls

if (isset($_GET['id'])){
    $url = $_GET['id'];
}

I check the value that variable $url is giving me as a result:

  

book-php /

Currently the data is obtained in such a way because I have the file .htaccess in Urls friendly.

The data is correct when making the query:

$stmtID = $con->prepare("SELECT idlibro,libro,titulo,precio FROM biblioteca WHERE url=?");

Now my question:

What is the value of id to $_GET ?

If I change in this way the code does not work, all the variables of the file lose their validation for the reason that they no longer receive the ideal result to the query.

if (isset($_GET['url'])){
    $url = $_GET['url'];
}

What does id have to do in $_GET ?

  

I do not have any product or data in the database that has a id column, all IDS are defined as id_producto , id_usuario etc.

Why does not giving it another name work the same?

  

Update

.htaccess file

#Regla Url amigable
RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?id=$1
    
asked by May 19.10.2017 в 14:48
source

3 answers

9

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.

answered by 19.10.2017 / 14:59
source
6

Short answer:

Change your .htaccess to the following:

 RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?url=$1

Long Response:

The variables $_GET in general terms are those that are sent by the URL would explain this but A.Cedano went to the trouble .

When you make "friendly URLs" via .htaccess or virtualhost you also send parameters via GET as you can see in your .htaccess

#Regla Url amigable
RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?id=$1

You are overriding the url and internally send the id :

detalle.php?id=$1

If you want to change this and receive url in your code you should change your virtualhost or .htaccess to the following:

RewriteRule ^([a-zA-Z0-9-/]+)$ detalle.php?url=$1
    
answered by 19.10.2017 в 16:00
3

What happens is that what you add in $_GET['nombre'] is the name of the variable that is being sent from the Front-End to the Back-End, in some file you are sending that variable with the name of id and this is how you should receive it, if you want to change the name you should then change it at the time of sending and receive it in php with the new name

I hope to have been clear in the explanation, likewise I leave you this Link with more information about the GET variables

    
answered by 19.10.2017 в 14:55