How to hide a URL ID

Asked

Viewed 333 times

0

I have a function that captures the ID of the URL and makes the query in DB and returns the value on the page, how to do this query without appearing the ID in the URL?

Currently this way:

https://domain.com/nome-do-post/? id=9991

How to Hide the ?id=9991 from the URL to is just like this:

https://dominio.com/nome-do-post/

I’ll leave the function here, but I think the function itself adds nothing to the question.

function protetor() {
    $conexao = mysqli_connect("127.0.0.1","root","","protetor");
    $id = $_GET['id'];
    $dados = mysqli_query($conexao, "SELECT * FROM download WHERE ID = $id");
    $download = mysqli_fetch_array($dados);
    if (!empty($id)) {
        echo $download['link'];
    } else {
        echo "Link de download não encontrado!";
    }
}
  • nome-do-post is a unique value for registration? To do what you want, it will need to be unique. Read on Slug and friendly Urls.

  • in case I add the ?id=9991 just so the function know what it has to look for in the table, this is not part of the post, doubt and how I can make the function detect this ID without I have to add it at the end of the URL

  • Exactly. Without the id, the only information will be nome-do-post, then that amount will have to be enough for you to find the registration in the bank.

  • Wouldn’t there be a way to pass this ID so it can be captured by a cookie and then take it directly from the cookie? like, redirect the ID this way, but after the page captures the ID it refreshes and saves the cookie, it would be possible to do this?

  • And why not do it the easy way (and correct)?

  • @Anderson being very honest with you, I have no idea how to do this, like, I’m not familiar with these HTACCESS rules, but thanks anyway, I’ll try to take advantage of that information.

  • In the first comment I gave you the tips of what you need to study. htaccess is one of the ways used. Focus first on understanding the concept, then try to apply it. If you go straight to htaccess and try to do without understanding what you are doing it will be a bad experience.

Show 2 more comments

1 answer

0

Good morning!

You have the option to stream through a POST instead of a GET

ex:

<input type="hidden" ....value="ID" />

You can also share the data by SESSION which is less recommended.

  • The moment you change page you can send a form sending the data by POST

  • 1

    What if you go to the direct URL? It will be a GET request, not POST. What you suggested is to gambiarra, use a tool for a very distinct purpose for which it was designed.

  • what I’m trying to do and a simple protector, I added the ?id=9991 at the end of the URL, just for the function to know what it has to look for inside the table, I want to redirect several different Ids to that page, but without having to show which ID

  • @Andersoncarloswoss It is a little vague to say that it would be a gambiarra, since it is an excellent treatment for data that should be "hidden". In this case, the landing page must have a controller to handle these situations

  • 1

    It’s not vague. You’d be submitting forms just to navigate between pages. Form was not designed for this and use it for such is gambiarra. Imagine the user accessing the page through the menu, who makes a POST sending the ID, every time he reloads the page the browser will ask if he wants to resend the information. If he says no, break the site because there will be no ID. It will also not be able to save in the bookmarks, because if you access in the future it will be a GET request, without the ID, not a POST.

  • 1

    @Andersoncarloswoss understood with the example you passed and your solution in the other comment. I believe that in this case the correct one would be with Slug (if there are repeated names). Thanks

Show 1 more comment

Browser other questions tagged

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