0

How can I hide the user id in the url using php?

I have a button with the user id:

<a href="edit.php?id<?php echo $row['user_id']; ?><button>Edit</button></a>

How can I hide or better encrypt it?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Edo Tensei
  • 31
  • 3

2 Answers2

-3

use flash session instead of $_GET parameters flash session is once set and when reading session destroy ownself

for example in laravel

return  redirect()->route('ffaa')->with('id',12);

here with method sends data to another page using flash session

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28
  • What is "flash session", exactly? – Dai Nov 05 '21 at 06:42
  • Flashmessages are a system of setting a (session) variable on page X and instantly removing it on a new a request – DarkBee Nov 05 '21 at 06:43
  • 1
    This solution won't suffice for OP, as the shown url is `edit.php` I'd imagine there are more than just one user record – DarkBee Nov 05 '21 at 06:45
  • You set flash session, you read it once and they disappear, that's it, next time you want to access it, it is no longer there, so sending data between pages is just like sending data with inter-activity intent on android. You can get information about flash session with a little google search. – dılo sürücü Nov 05 '21 at 06:45
  • The answer is related to laravel Framework. But the user has asked the question using core php – ManojKiran A Nov 05 '21 at 06:51
  • i just gave an example from laravel, i know you are not using laravel – dılo sürücü Nov 05 '21 at 06:53
  • Please add some explanation to your answer such that others can learn from it. If there were multiple links with different IDs, how should your approach work? Which kind of link would you put in the markup? – Nico Haase Nov 05 '21 at 07:59
-3

You can encrypt User ID in href

<?php $user_id = md5($row['user_id']); ?>
<a href="edit.php?id=<?php echo $user_id; ?>"><button>Edit</button></a>

and on edit.php page you can validate it using same encryption method, like:

<?php 
   $user_id = $_GET['id'];
   if($user_id === md5($row['user_id'])){
       // Code user id matches.
   }
?>
Sourav Dutt
  • 70
  • 1
  • 8
  • 1
    md5 is not an encryption algorithm. Also, as this is not reversible, you would need to fetch **all** rows from the database to find the proper one – Nico Haase Nov 05 '21 at 07:59
  • @NicoHaase you're right, I was just giving him a hint. He can use anything else, like **openssl_encrypt()** to encrypt it in **href** and on edit.php he can decrypt it using **openssl_decrypt()** – Sourav Dutt Nov 05 '21 at 08:16
  • Then why not mention such an approach in your answer? – Nico Haase Nov 05 '21 at 08:18