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的問題,