为PHP中的每个条目创建唯一的URL

So i am trying to create independent web pages via PHP for practice im trying to learn.

What i want to do is to create unique URLs for each entry on my site.

Lets say my website is simple, it just asks the user to enter a name and records it to the database, and creates a unique URL for that user so he can go to his url and view his name.

I am looking to create a URL for each user lets say upto 8 characters a-z A-Z 0-9 that would look like url.com/uI53841a

So if John goes to create a website it creates a url he can visit url.com/uI53841a for example.

My database would look like this below:

create table if not exists `entry` (
    `uniqueURL` VARCHAR(8),
    `name` VARCHAR(16),
    primary key (`uniqueURL`)
) engine=innodb default charset=utf8;

How would i go about generating a unique ID for each person who submits their name to my site? And how do i go about making sure there are never any duplicates?

I am new to PHP and trying to learn, i see you can create md5 hashses but how can you randomize that and check theres no duplicates?

Checkout this library.

Small snippet from docs:

<?php

$hashids = new Hashids\Hashids();

$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

var_dump($id, $numbers);

More info from official site.

P.S.

With primary key contrait for uniqueUrl you may check the result of insert with php. If it fails, it means that constrait is violated.

You may simply generate url based on other unique string, for example use raw id as url (as Dagon said in comments to the answer). But you should use aforementioned lib if you do not want to expose your database ids to the user (it's quote from github).

Heres one i found and worked on a little but which also works well!

function keygen()
{
    $chars = "abcdefghijklmnopqrstuvwxyz";
    $chars .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $chars .= "0123456789";
    while (1) {
        $key = '';
        srand((double) microtime() * 1000000);
        for ($i = 0; $i < 8; $i++) {
            $key .= substr($chars, (rand() % (strlen($chars))), 1);
        }
        break;
    }
    return $key;
}