2

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.

Accumulator
  • 873
  • 1
  • 13
  • 34

3 Answers3

3

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.

Community
  • 1
  • 1
2
  1. To generate random path you could use PHP's built-in uniqid() function

  2. 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.

  3. 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.

Denis Mysenko
  • 6,366
  • 1
  • 24
  • 33
1

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

Billy N.
  • 21
  • 4