从PHP中的URL获取用户名 [英] Get username from URL in PHP

查看:141
本文介绍了从PHP中的URL获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

http://www.someniceandreliableurl.com/username

并捕获用户名.

我想制作类似twitters/facebook/etc的快速网址...

I want to make something like twitters/facebook/etc quick urls...

twitter.com/username

我如何用php做类似的事情? =)预先谢谢您.

How can i make something like this with php? =) thank you in advance.

推荐答案

.htaccess 文件中使用 mod_rewrite :

RewriteEngine on
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]
RewriteRule ^(.*)$ /foo.php/$1 [QSA,L]

foo.php 是您用于显示快速url页面的脚本.

Where foo.php is your script for showing the quick url pages.

然后在 foo.php (或您命名的任何名称)中,您可以在$_SERVER['PATH_INFO']中捕获用户名.

Then in foo.php (or whatever you named it) you can catch the username in $_SERVER['PATH_INFO'].

前5行打开 mod_rewrite ,并允许实际的文件/文件夹/脚本被重写规则忽略.最后一个重写所有不是罚款/文件夹/脚本的网址.

The first 5 lines turn on mod_rewrite and allow actual files/folders/scripts to be ignored by the rewrite rule. The last one rewrites all the urls that aren't fines/folders/scripts.

这篇关于从PHP中的URL获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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