Question about PHP urls

I feel that this question might be pretty elementary, but I have no doubt someone here can help educate me. Basically what I have made is a simple web application that generates a random photo and accompanying text for someone when they click a button (a random funny tarot reading; everything is stored in the mysql database). Currently the page does not display any URL relating to the database row that is displayed. Everything simply appears within index.php.

The issue is that I would like people to be able to share these on social media, so the one they receive becomes what is shared (instead of just the index.php page where everything is generated). An obvious solution, which I have considered, is to simply display the result according to its id (for example, the url would be

The issue, though, is that I also don’t want people to simply be able to go into the browser and change the id number so they can just look through all of the entries in order by changing the id number. I would like everything to remain random.

The other reason I want to display them somehow individually is so I can ultimately add comments functionality or ratings for each specific entry.

My question, then: is there any simple way to change the url of the results so there is no manual searching? I hope I have been able to articulate what my problem/aim is here. I am more than happy to clarify anything that doesn’t make sense.

I think if you want to have it be retrievable “by URL” it will need to be by using GET variables.

-BUT- that doesn’t mean it has to be sequential id numbers.

I guess a complete list could be compiled over time if one wanted to, but using some kind of random hash type value (maybe more than 1 ?) would make it harder.

1 Like

So basically all I need to do is add a new column to the db table for this random string and use that instead of the id. I guess my only question now is how would I make each one unique and auto generated whenever I add a new entry?

It doesn’t even need to be anything spectacular, just less obvious to the user than the id numbers.

Disregard. Since the id number is the primary key and unique, I can just hash that into the new column and the result will always be unique. :slight_smile: Thanks!

1 Like

Hashes by definition are not unique - all that a hash guarantees is that a small change in the starting value gives a large change in the hash - two numbers a long distance apart could possibly result in the same hash (so you would still need to test to ensure that the hash generated is actually unique - by specifying that column as unique in the DB and incrementing the id again if you ever get one that fails to insert.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.