本文目錄一覽:
php處理url的幾個函數
pathinfo
[php] view plaincopy
?php
$test = pathinfo(“”);
print_r($test);
?
結果如下
Array
(
[dirname] = //url的路徑
[basename] = index.php //完整文件名
[extension] = php //文件名後綴
[filename] = index //文件名
)
parse_url
[php] view plaincopy
?php
$test = parse_url(“;sex=1#top”);
print_r($test);
?
結果如下
Array
(
[scheme] = http //使用什麼協議
[host] = localhost //主機名
[path] = /index.php //路徑
[query] = name=tanksex=1 // 所傳的參數
[fragment] = top //後面根的錨點
)
basename
[php] view plaincopy
?php
$test = basename(“;sex=1#top”);
echo $test;
?
結果如下
index.php?name=tanksex=1#top
url直接訪問Php中方法
這是不可能的,你應該要把它實例化,
例如
myclass.php
?php
class test{
public function abc(){
echo “this is the function of abc”;
}
}
//實例化
$mytest=new test();
//執行方法
$mytest-test();
當你瀏覽器輸入這個網站的路徑+myclass.php的時候,頁面就會顯示
this is the function of abc
php如何通過url調用php文件中的方法
題主所描述的這種形式,是MVC設計模式的典型應用。
通過使用PSR4來實現自動加載,可以通過處理路由來實現
//處理路由的方法
static public function route()
{
//獲取的模塊
$_GET[‘m’] = isset($_GET[‘m’]) ? $_GET[‘m’] : ‘Index’;
//獲取行為動作action 又叫方法
$_GET[‘a’] = isset($_GET[‘a’]) ? $_GET[‘a’] : ‘index’;
$controller = ‘Controller\\’ . $_GET[‘m’] . ‘Controller’;
//echo $controller;
$c = new $controller();
//$c-$_GET[‘a’]();
call_user_func(array($c , $_GET[‘a’]));
}
最終可實現以下形式:
如何通過PHP獲取當前頁面URL函數
通過PHP獲取當前頁面URL函數代碼如下,調用時只需要使用 curPageURL() 就可以:
/* 獲得當前頁面URL開始 */
function curPageURL() {
$pageURL = ‘http’;
if ($_SERVER[“HTTPS”] == “on”) { // 如果是SSL加密則加上“s”
$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;
}
/* 獲得當前頁面URL結束 */
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/245044.html