Variable storage in PHP - $ _SETION x $ _GET [closed]

1

Hello friends. I have a question about good practices applied in development: What would be the best way to store temporary variables in an application? I have a PHP application where I can edit users, where I just click the link and it directs me to the edit page where the ID is retrieved through the user query string.

http://www.app.com.br?usuario=23

Would this be the best way? Or would it be better to store it in a session variable? Thanks for the help!

    
asked by anonymous 26.01.2017 / 14:01

1 answer

3

With $_GET you will only have access to the variable, in this case with value 23 in service http://www.app.com.br?usuario=23 , because in http://www.app.com.br?usuario=24 you will no longer have your variable with 23 value, but yes 24 .

That said, to temporarily save a variable across your entire application, you should use session or < a href="http://www.w3schools.com/php/php_cookies.asp"> cookies .

In this case if you want to save the value $_GET['usuario'] in a session, you do (this is a very basic way):

<?php
session_start();
$_SESSION['usuario'] = $_GET['usuario'];

In which you will have access on all other pages, if you place them on top of them all session_start() .

But this does not make much sense to me (so I realized), because if the user then goes to url http://www.app.com.br?usuario=24 , the variable $_SESSION['usuario'] will now have the value 24 .

My tip, if user 23 can not see http://www.app.com.br?usuario=24 is to save this id from login and check this page:

<?php
session_start();
if($_SESSION['usuario'] != $_GET['usuario']) {
    header('Location: OUTRA_PAG.php');
    die();
}
    
26.01.2017 / 14:09