首页 > 解决方案 > 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。获得第一行,我也在报价中使用瑞典字母。

标签: php

解决方案


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.


推荐阅读