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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么