php - PHP GET,使用URL友好而不是来自ID?
问题描述
我有一个这样的 .txt 文件(大约 200 行)
Fjärilar smakar med fötterna.
Ett normalt badkar rymmer ca 200 liter.
有了这个 PHP 代码,有人可以使用像http://example.com.com/?quote=2这样的链接,他们会得到第二个报价,如果他们访问http://example.com.com,就会得到一个随机报价。如果用户随机获取,我会更新 URL(将用户转发到 id URL),使其能够共享链接并返回报价。
$f_contents = file("facts.txt");
if(!isset($_GET['quote']) || !is_numeric($_GET['quote']) || $_GET['quote'] > count($f_contents)){
$random = array_rand($f_contents);
$line = $f_contents[$random];
header("Location: ?quote=$random");
die();
}else{
$line = $f_contents[$_GET['quote']];
}
如何将此 ut 转换为使用 URL 友好而不是来自 ID?我用户使用链接http://example.com.com/?quote=fjarilar-smakar-med-fotterna。获得第一行,我也在报价中使用瑞典字母。
解决方案
You can maintain a database table where for each entry of id have a column named 'friendly_url' so based on this info you can write URL rewriting rules or code to redirect the same.
推荐阅读
- mysql - 返回在两列中出现多次的年份值
- javascript - 动态更改 HTML 标题
- project-reactor - Project Reactor:为每个订阅的发布者缓存最后一项
- c# - 在 C# 中随机化字符串
- haskell - foldr 和 zipWith (:) 如何一起工作?
- spring-boot - Gradle、Kotlin、多模块项目:编译此项目时出现构建错误
- javascript - 如何将 CSS 导入 React (electron.js) 项目?
- mongodb - 如何在 Kubernates 集群中创建单个 MongoDB 容器
- quicksort - 一个元素被/可以选择多少次作为快速排序中的枢轴?
- html - 带按钮的数字输入