dourong8495 2015-10-15 02:21
浏览 56
已采纳

为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?

  • 写回答

2条回答 默认 最新

  • dtpyvb1873 2015-10-15 02:29
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了