您好,欢迎访问一九零五行业门户网

PHP怎么获取url里的自定义参数

php怎么获取url里的自定义参数,
比如:
http://www.xxx.com/cba/index.php http://www.xxx.com/abc/index.php http://www.xxx.com/bbb/index.php 分别出来的值是:
cba abc bbb 即
http://www.xxx.com/{shop}/index.php
需要获取shop这个变量
回复内容: php怎么获取url里的自定义参数,
比如:
http://www.xxx.com/cba/index.php http://www.xxx.com/abc/index.php http://www.xxx.com/bbb/index.php 分别出来的值是:
cba abc bbb 即
http://www.xxx.com/{shop}/index.php
需要获取shop这个变量
apache大概这样配置
aliasmatch ^/(匹配你参数的正则表达式)/index.php /path/to/process.php?arg=$1
然后在process.php文件里$_get['arg'] 就是
告诉一种用过的愚蠢的办法吧
$_server['php_self']取出,然后正则,详情看以看看这个链接
外加,一般php程序都是用框架写的把,那样的话你可以去他的文档找他定义的常量即可,实在没有就是上面的
方案1. 使用服务器的uri重写功能,参考@zonxin的答案
方案2. 在php中去解析url,注意应该使用$_server['request_uri'],例如:
if (preg_match('|^/([^/]+)/index.php|', $_server['request_uri'], $matches)){ list(, $shop) = $matches; echo $shop; // => 你期望的shop这个变量 }
b.t.w. 关于request_uri和php_self的区别,请参考这篇讨论
.htaccess重写url
isset($_server['path_info'])?explode('/',my_addslashes(ltrim(trim($_server['path_info']),'/')))
可以使用path_info获取,不过不建议这么做了,现在的框架都是使用query_string
获取path_info,用正则匹配出来
preg_match('/\/(.*)\/index.php/',$_server['path_info'],$match);var_dump($match[1]);
其它类似信息

推荐信息