I'm making a mini-paste-website for myself, kind of like pastebin but much simpler. I need it to generate a random url (something like mysite.com/paste/ab8536dc2e, mysite.com?id=b91527a2ac, etc. format doesn't really matter) with the paste in it. How would I generate a url like that and have it lead to a page with something I submitted in it? I'm using PHP, I'd prefer to keep it as simple and plain as possible and not use anyting like MySQL, jQuery etc. unless needed.
-
https://paragonie.com/blog/2015/09/comprehensive-guide-url-parameter-encryption-in-php – Scott Arciszewski Jan 11 '16 at 06:26
-
I'm not encrypting anything. I'm literally just making pseudo-randomly-generated URLs for each 'paste'. – Accumulator Jan 12 '16 at 19:02
-
Sure, I had read "not use anything like MySQL" to mean "no persistent storage" to map your pseudorandom URLs to each paste. – Scott Arciszewski Jan 12 '16 at 19:37
3 Answers
You can use the the following function :
function randomURL($URLlength = 8) {
$charray = array_merge(range('a','z'), range('0','9'));
$max = count($charray) - 1;
for ($i = 0; $i < $URLlength; $i++) {
$randomChar = mt_rand(0, $max);
$url .= $charray[$randomChar];
}
return $url;
}
Call randomURL() to access it.
(Disclaimer: This is not my code, I "borrowed" it for a similar problem.)
Check ( Php create a file if not exists ) for using that string to create a file on your site.

- 1
- 1

- 41
- 3
To generate random path you could use PHP's built-in uniqid() function
You need to store the list of generated paths (links) somewhere. If you don't want MySQL – it could be SQLite, Memcached or even a plain-text file. But you have to keep them somewhere.
Depending on your chosen framework, you need to setup routing – check that the link exists (see 2) and then display the appropriate content. If you decided not to use any framework, you can simply work around $_SERVER['REQUEST_URI'] global variable.

- 6,366
- 1
- 24
- 33
-
Instead of `uniqid()` I'd recommend building a string out of `random_int()`. – Scott Arciszewski Jan 11 '16 at 06:26
You can use something like this and use the variable however you wish.
for ($i=0; $i < 10; $i++) {
$randomSite = "http://my.site/paste/" . hash('adler32', $i);
}
Where $i can be anything you want, such as the total number of paste. The for loop is not needed

- 21
- 4