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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus