PHP 动态数据库页面重写 URL [英] PHP dynamic DB page rewrite URL

查看:13
本文介绍了PHP 动态数据库页面重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作 www.mydomain.com/folder/?id=123 ---> www.mydomain.com/folder/xCkLbgGge

我希望我的数据库查询页面获得它自己的 URL,就像我在 twitter 等上看到的一样.

解决方案

这被称为slug" wordpress 使这个术语流行起来.无论如何.

最终您需要做的是拥有一个 .htaccess 文件来捕获所有传入的流量,然后在服务器级别对其进行改革以在某种意义上与您的 PHP 一起使用,您仍将保持 ?id=123 逻辑不变,但是到客户端 '/folder/FHJKD/' 将是可见的结果.

这里是一个 .htaccess 文件的例子,我在..上使用了类似的逻辑(wordpress 也是如此).

RewriteEngine On#如果存在,则将 www 从域中剥离RewriteCond %{HTTP_HOST} ^www.domain.com$#applies 从 http://mydomain.com/post/my-article 更改域的逻辑#类似于 http://mydomain.com/?id=post/my-article重写规则 ^(.*)$ http://domain.com/$1 [R=301,L]RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f重写规则 ^(.*)$ index.php?id=$1 [QSA,L]

这将执行 domain.com/之后的所有内容,并将其作为变量传递给 index.php,此示例中的变量将是id",因此您必须设置最适合您网站需求的逻辑.

示例

';打印_r($myParams);echo '</pre>';}?>

现在这个逻辑必须更深入,这只是基本级别的纯粹示例,但总体而言,尤其是我假设您使用数据库时,您会想要确保 $myParams 没有恶意代码,可以注入您的 PHP 或数据库.

上述 $myParams 通过 print_r() 的输出将是:

数组([0] =>邮政[1] =>我的文章)

要使用它,您至少需要做

echo $myParams[0].'
';

或者你可以这样做,因为大多数浏览器会添加一个 final/

 1){$sql = "SELECT * FROM post_table WHERE slug = '".mysql_real_escape_string($myParams[1])."'";$result = mysql_query($sql);}}?>

现在这无疑是一个非常粗略的例子,您可能希望在其中使用一些逻辑来防止 mysql 注入,然后您将应用查询,就像您现在使用 id=123 提取文章一样.

或者,您也可以走完全不同的路线,探索 MVC(模型视图控件)的奇迹.像 CodeIgniter 这样的东西是一个很好的简单的 MVC 框架,可以开始使用.但这取决于你.

How can I make www.mydomain.com/folder/?id=123 ---> www.mydomain.com/folder/xCkLbgGge

I want my DB query page to get it's own URL, like I've seen on twitter etc etc.

解决方案

This is known as a "slug" wordpress made this term popular. Anyway though.

Ultimately what you need to do is have an .htaccess file that catches all your incoming traffic then reforms it at the server level to work with your PHP in the sense, you will still keep the ?id=123 logic intact, but to the client side '/folder/FHJKD/' will be the viewable result.

here is an example of an .htaccess file I use a similar logic on.. (so does wordpress for that matter).

RewriteEngine On
#strips the www out of the domain if there
RewriteCond %{HTTP_HOST} ^www.domain.com$

#applies logic that changes the domain from http://mydomain.com/post/my-article
#to resemble http://mydomain.com/?id=post/my-article
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [QSA,L]

what this will do is take everything after domain.com/ and pass it as a variable to index.php the variable in this example would be 'id' from this you have to device a logic that best suits your sites needs.

example

<?php
 //the URL for the example here: http://mydomain.com/?id=post/my-article
 if($_GET['id'])
 {
   $myParams = explode('/', $_GET['id']);
   echo '<pre>';
   print_r($myParams);
   echo '</pre>';
 }
?>

now the logic for this would have to go much deeper, this is only pure example at a basic level, but overall and especially cause your working with a database I assume, your gonna wanna make sure the $myParams is clean of malicious code, that can inject into your PHP or Database.

The output of the above $myParams via print_r() would be:

Array(
   [0] => post
   [1] => my-article
)

To work with it you would need to do at the very least

echo $myParams[0].'<br />';

or you could do it like this cause most browsers will add a final /

<?php
 //the URL for the example here: http://mydomain.com/?id=post/my-article
 if($_GET['id'])
 {
   //breaks the variable apart, removes any empty array values and reorders the index
   $myParams = array_values(array_filter(explode('/', $_GET['id'])));
   if(count($myParams > 1)
   {
       $sql = "SELECT * FROM post_table WHERE slug = '".mysql_real_escape_string($myParams[1])."'";
       $result = mysql_query($sql);
   }

 }
?>

Now this admitedly is a very crude example, you would want to work some logic in there to prevent mysql injection, and then you will apply the query like you would how you are now in pulling your articles out using just id=123.

Alternatively you could also go a completely different route, and explore the wonders of MVC (Model View Control). Something like CodeIgniter is a nice easy MVC framework to get started on. But thats up to you.

这篇关于PHP 动态数据库页面重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆