我想知道是否可以只提取用户名。
以下是我正在使用的一个示例路径:
/home/UserName/public_html/index.php
文件夹的IP链接中使用了相同的用户名。下面是我使用的一个示例。http://xxx.xxx.xxx.xxx/~用户名/index.php
我想知道我是否可以以某种方式使用php从服务器获取此路径,然后解析出除usrername之外的所有内容。然后,我想把这个用户名作为一个变量,我可以在页面上的其他地方使用它。
$username = UserName
这有道理吗?
我遇到的根本问题是,我使用了很多php includes,并且我遇到了绝对/相对路径的问题。我想,通过获得用户名,我可以让一切都成为一条绝对的路径。也许我做错了。
任何建议都会很棒。谢谢大家抽出时间。
AFAIK,最佳做法是使用$_SERVER['DOCUMENT_ROOT']
进行include。
include($_SERVER['DOCUMENT_ROOT'] . '/templates/footer.php');
$path = $_SERVER['REQUEST_URI']; // this gives you /folder1/folder2/THIS_ONE/file.php
$folders = preg_split('/', $path); // splits folders in array
$what_we_need = $folders[3];
从http://www.sitepoint.com/forums/showthread.php?192267-如何设置文件夹名称URL外
明白了!这是代码:
<?php
$full = dirname(__FILE__);
$folder = basename(dirname(__FILE__));
$trim = strtr($full, array('/home/'=>''));
$trim2 = strtr($trim, array('/public_html/'=>''));
$userid = strtr($trim2, array($folder=>''));
?>
在本例中,如果
dirname(__FILE__) = /home/USERNAME/public_html/blog/
然后$userid将=用户名