phpurl路径
⑴ 如何在php中实现URL路由
第一步,首先要在服务器的配置上对/router/路径进行拦截
调用某个文件夹目录下的index.php页面,假定现在所有模块使用单独的文件存放于class目录下,该目录与router平级,如下图所示:
第二步,路由分发器的实现(index.php)
1: <!Doctype html>
2: <html>
3: <head>
4: <title>路由测试~~</title>
5: <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6: </head>
7: <body>
8:
9: <?php
10:
11: date_default_timezone_set("Asia/Shanghai");
12:
13: define("MODULE_DIR", "../class/");
14:
15:
16: $_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
17: $_FilePath = __FILE__;
18: $_RequestUri = $_SERVER['REQUEST_URI'];
19:
20: $_AppPath = str_replace($_DocumentPath, '', $_FilePath); //==>\router\index.php
21: $_UrlPath = $_RequestUri; //==>/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
22:
23: $_AppPathArr = explode(DIRECTORY_SEPARATOR, $_AppPath);
24:
25: /**
26: * http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
27: *
28: * /hello/router/a/b/c/d/abc/index.html?id=3&url=http:
29: */
30:
31: for ($i = 0; $i < count($_AppPathArr); $i++) {
32: $p = $_AppPathArr[$i];
33: if ($p) {
34: $_UrlPath = preg_replace('/^\/'.$p.'\//', '/', $_UrlPath, 1);
35: }
36: }
37:
38: $_UrlPath = preg_replace('/^\//', '', $_UrlPath, 1);
39:
40: $_AppPathArr = explode("/", $_UrlPath);
41: $_AppPathArr_Count = count($_AppPathArr);
42:
43: $arr_url = array(
44: 'controller' => 'index',
45: 'method' => 'index',
46: 'parms' => array()
47: );
48:
49: $arr_url['controller'] = $_AppPathArr[0];
50: $arr_url['method'] = $_AppPathArr[1];
51:
52: if ($_AppPathArr_Count > 2 and $_AppPathArr_Count % 2 != 0) {
53: die('参数错误');
54: } else {
55: for ($i = 2; $i < $_AppPathArr_Count; $i += 2) {
56: $arr_temp_hash = array(strtolower($_AppPathArr[$i])=>$_AppPathArr[$i + 1]);
57: $arr_url['parms'] = array_merge($arr_url['parms'], $arr_temp_hash);
58: }
59: }
60:
61: $mole_name = $arr_url['controller'];
62: $mole_file = MODULE_DIR.$mole_name.'.class.php';
63: $method_name = $arr_url['method'];
64:
65: if (file_exists($mole_file)) {
66: include $mole_file;
67:
68: $obj_mole = new $mole_name();
69:
70: if (!method_exists($obj_mole, $method_name)) {
71: die("要调用的方法不存在");
72: } else {
73: if (is_callable(array($obj_mole, $method_name))) {
74: $obj_mole -> $method_name($mole_name, $arr_url['parms']);
75:
76: $obj_mole -> printResult();
77: }
78: }
79:
80: } else {
81: die("定义的模块不存在");
82: }
83:
84:
85: ?>
86:
87: </body>
88: </html>
获取请求的uri,然后拿到要加载的模块名、调用方法名,对uri参数进行简单的判断..
第三步,模块的编写
根据上述的uri,我们要调用的是Hello模块下的router方法,那么可以在class目录下定义一个名为Hello.class.php的文件(注意linux下是区分大小写的)
1: <?php
2:
3: class Hello {
4:
5: private $_name;
6: private $_varValue;
7:
8: function __construct() {
9:
10: }
11:
12: function router() {
13: $this->_name = func_get_arg(0);
14: $this->_varValue = func_get_arg(1);
15: }
16:
17: function printResult() {
18: echo $this->_name;
19: echo "<p>";
20: echo var_mp($this->_varValue);
21: echo "</p>";
22: }
23: }
24:
25: ?>
⑵ php中的url路径index.php不能跳转
你这用框架了吗含扰培?李腊要是用框架谈唯可以修改路由规则
没用框架你可以定义个全局变量试试,那样的话可能需要字符串拼接
⑶ php如何获取当前页面url路径
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on")
{
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] .
$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"] .
$_SERVER["REQUEST_URI"];
}
return $pageURL;}?>
(3)phpurl路径扩展阅读:
获取域名或主机地址 :echo $_SERVER['HTTP_HOST'].""; #localhost
获取网页地址:echo $_SERVER['PHP_SELF'].""; #/blog/testurl.php
3.获取网址参数:echo $_SERVER["QUERY_STRING"].""; #id=5
4.获取用户代理:echo $_SERVER['HTTP_REFERER']."";
⑷ php中匹配URL路径
一瞧就是做当前链接css样式的
换下衡中思路就好,不需要用preg_match来实现辩拦游的
if($_GET['page_id']==143)echo"active";
//携销用preg_match效率并不高,hi-docs.com/php/preg_match.html
⑸ thinkphp url路径怎么加上index.php
了简化操作,我们经常使用“模板替换”来定义个性化的路径,如:
//模板替换
'TMPL_PARSE_STRING' =>array(
'__BOOTSTRAP__' => SITE_URL.'/Public/bootstrap3.3',//bootstrap路径规则
'__UPLOAD__' => SITE_URL.'/Uploads', // 上传路径替换规则
'__ADDONS__'=>SITE_URL.'/Addons',//插件目录
//公共资源文件
'__COMMONJS__' => SITE_URL.'/Public/Common/Js',
'__COMMONCSS__' => SITE_URL.'/Public/Common/Css',
),1234567891012345678910
上面的SITE_URL指的就是网站的uri路径。
以前的时候我是直接在index.php中定义这个常量,后来发现这样做在移植和上传的时候很麻烦,于是有了下脊侍面的这一段代码。
//定义常量
define('SITE_NAME','');
$the_file_path = $_SERVER['PHP_SELF'];
$findme = '/index.php';
$pos = strpos($the_file_path, $findme);
$target_path = substr($the_file_path, 0,$pos);
$site_url = "http://樱亩吵".$_SERVER['HTTP_HOST'耐高].$target_path;
define('SITE_URL',$site_url);
⑹ windows下 php+CI+apache 怎么删除URL路径中的index.php
默认情况下,index/index.php/news/article/my_article 你可以很容易的通过 .htaccess 文件来设置一些简单的规则删除它。下面是一个例子,使用“negative”方法将非指定内容进行重定向: RewriteEngine on RewriteCond $1 !^(index\.phpimagesrobots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] 注意:如果你的项目不在根目录请把上面这一句改为:RewriteRule ^(.*)$ index.php/$1 [L] 在上面的例子中,可以实现任何非 index.php、images 和 robots.txt 的 HTTP 请求都被指向 index.php。 我的终极解决方案 但在实践中,以上方案仅适用于与运行于Apache环境下的服务器且并不具有充分的普遍适用性!当CI程序位于非根目录或位于某些虚拟主机上时,以上解决方案会引起”404错误”或”no input file specified”等错误.网络参考过相关问题的解放方案后,找到了一种具有通用性的有效删除URL路径中index.php的方法,代码参考如下: index位于根目录时,你可以在index.php所在的根目录里新建.htaccess文件并使用以下代码: RewriteEngine on RewriteCond $1 !^(index\.phprobots\.txt) RewriteRule ^(.*)$ /index.php?/$1 [L] 当index.php不在根目录时,你可以在index.php所在目录里新建.htaccess文件并使用以下代码: RewriteEngine on RewriteCond $1 !^(index\.phpimagesrobots\.txtl) RewriteRule ^(.*)$ /path_to_app/index.php?/$1 [L] 注意把path_to_app换成你的index.php所在文件的目录.假设你的index.php放在根目录的tool子文件夹下,你可以这样写: RewriteEngine on RewriteCond $1 !^(index\.phpimagesrobots\.txtl) RewriteRule ^(.*)$ /tool/index.php?/$1 [L] 以上方案应该可以解决Apache环境下如何有效删除URL中index.php的问题,