简体   繁体   English

如何在php中给url一个唯一的ID?

[英]how to give url a unique id in php?

I want give particular unique id in my url, like at the end of url "?id=smtb" should be included... 我想在网址中提供特定的唯一ID,例如应在网址末尾添加“?id = smtb” ...

as I am not knowing which code to be used in order to convert below url 因为我不知道要使用哪个代码来转换下面的URL

www.mylink.com/page.php

into

www.mylink.com/page.php?id=smtb
www.mylink.com/page.php?id=sea

so that I can able to store in my database 这样我就可以存储在数据库中

Contact Form 联系表

<form method="GET" action="process.php">
    <input type="hidden" name="p" id="p" value="<?php echo $_GET['id']; ?>" />
</form>

process.php process.php

$pageid = $_GET['p'];

When you click the link, you wil get something like: 当您单击链接时,将得到类似以下内容的信息:

www.abc.de/process.php?p=123123 this part makes no sense for me tho: www.abc.de/process.php?p=123123这部分对我来说毫无意义,例如:

<?php echo $_GET['id']; ?> 

probably its this: 大概是这样的:

<?php echo $id ?> 

You can create uniq id's with this following function in PHP. 您可以在PHP中使用以下函数创建uniq id。

$up_id = uniqid(); 

If you just want your random id in Just the letters like string format. 如果您只想以字符串之类的字母表示随机ID。

You can uset that following function definied. 您可以使用以下定义的功能。

function generateRandomString($length = 5) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

You can get the link with linking like <a href="page.php?id=<?php echo $up_id; ?>">here you go</a> 您可以通过<a href="page.php?id=<?php echo $up_id; ?>">here you go</a>

If I understood your question correctly, you are looking for a way to generate a unique (numeric) id for each form you submit? 如果我正确理解了您的问题,那么您正在寻找一种为提交的每种表单生成唯一(数字)ID的方法吗?

The first thing that comes to mind is to use a time function, which technically could result in duplicates, but in practice will be safe unless you have over 1000 requests per second. 首先想到的是使用时间函数,从技术上讲,它可能导致重复,但是实际上除非您每秒有1000个以上的请求,否则它是安全的。

$id = microtime(true) * 10000;

this snippet will produce a number similar to: 13966142246941 此摘录将产生类似的数字: 13966142246941

As for including it in your form, I'm guessing that @Xatenev's answer will suffice. 至于将其包含在您的表单中,我猜测@Xatenev的答案就足够了。

<?php echo $id ?>

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM