php頁面url
❶ 如何在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 CodeIgniter框架中怎麼獲取當前頁面的URL
1.
默認情況,你的
url
中會包含 index.php 文件:
如果你的
apache
伺服器啟用了 mod_rewrite ,你可以簡單的通過一個
.htaccess
文件再加上一些簡單的規則就可以移除
index.php
了。下面是這個文件的一個例子,
其中使用了
"否定條件"
來排除某些不需要重定向的項目:
rewriteengine on
rewritecond %{request_filename} !-f
rewritecond %{request_filename} !-d
rewriterule ^(.*)$ index.php/$1 [l]簡單的說就是:在網站根目錄新建一個名字為:「.htaccess」的文件,文件內容拷貝上面4行代碼即可!
2.
如果你使用的iis伺服器的話,需要安裝rewrite模塊,可以去網路一找一下,很多
❸ php打開URL的幾種方法
PHP中打開URL地址的幾種方法總結,這里的函數主要用於小偷採集等函數。
1:用file_get_contents
以get方式獲取內容
復制代碼代碼如下:
<?php
$url='http://www..com/';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>
示例代碼2:用fopen打開url,
以get方式獲取內容
復制代碼代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"urlbody:$result";
printhr();
fclose($fp);
?>
示例代碼3:用file_get_contents函數,以post方式獲取url
復制代碼代碼如下:
<?php
$data=array('foo'=>
'bar');
$data=http_build_query($data);
$opts=array(
'http'
=>array(
'method'=>'POST',
'header'=>"Content-type:
application/x-www-form-urlencoded".
"Content-Length:".strlen($data).
"",
'content'=>$data
),
);
$context=
stream_context_create($opts);
$html=
file_get_contents('http://localhost/e/admin/test.html',false,$context);
echo$html;
?>
示例代碼4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body
復制代碼代碼如下:
<?
functionget_url
($url,$cookie=false){
$url=parse_url($url);
$query=
$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen(
$url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1";
$request.="Host:$url[host]";
$request.="Connection:Close";
if($cookie)$request.="Cookie:$cookie ";
$request.="";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,
1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
$body=
stristr($rowdata,"");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?>
❹ php取頁面所有的URL怎麼實現
function get_all_url($code){ preg_match_all(『/"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i』,$code,$arr); return array(『name』=>$arr[2],』url』=>$arr[1]);}
❺ php如何獲取當前頁面url路徑
#測試網址:     http://localhost/blog/testurl.php?id=5
//獲取域名或主機地址 
echo $_server['http_host']."
"; #localhost
//獲取網頁地址 
echo $_server['php_self']."
"; #/blog/testurl.php
//獲取網址參數 
echo $_server["query_string"]."
"; #id=5
//獲取用戶代理 
echo $_server['http_referer']."
"; 
//獲取完整的url
echo 'http://'.$_server['http_host'].$_server['request_uri'];
echo 'http://'.$_server['http_host'].$_server['php_self'].'?'.$_server['query_string'];
#http://localhost/blog/testurl.php?id=5
//包含埠號的完整url
echo 'http://'.$_server['server_name'].':'.$_server["server_port"].$_server["request_uri"]; 
#http://localhost:80/blog/testurl.php?id=5
//只取路徑
$url='http://'.$_server['server_name'].$_server["request_uri"]; 
echo dirname($url);
#http://localhost/blog
❻ 如何通過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結束 */
❼ 原生PHP如何獲取當前頁面URL
functionget_current_url(){
$current_url='http://';
if(isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'){
$current_url='https://';
}
if($_SERVER['SERVER_PORT']!='80'){
$current_url.=$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
}else{
$current_url.=$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}
return$current_url;
}希望能幫到你
❽ php頁面跳轉的時候url會自己變
跳轉
header()為php函數,向瀏覽器發送指定命令
html:
<meta http-equiv="Refresh" content="3;url=other.php"/>        
立即跳轉:
header('Location:other.php');  //file_put_contents('bee.txt','execute');  die;        
執行header時候,並不是立即結束,而是會把頁面執行完畢;在header前面不能有任何輸出,若有開啟輸出緩沖則不提示錯誤,php.ini->output_buffering = 4096|OFF
提示跳轉:
header('Refresh:3,Url=other.php');  echo '3s 後跳轉';  //由於只是普通頁面展示,提示的樣式容易定製  die;        
封裝的跳轉函數:
/*   *跳轉   *@param $url 目標地址   *@param $info 提示信息   *@param $sec 等待時間   *return void  */  function jump($url,$info=null,$sec=3)  {   if(is_null($info)){    header("Location:$url");   }else{    // header("Refersh:$sec;URL=$url");    echo"<meta http-equiv=\"refresh\" content=".$sec.";URL=".$url.">";    echo $info;   }   die;  }        
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP網路編程技巧總結》、《PHP基本語法入門教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
❾ 問如何通過php獲取提交頁面的URL
樓上兩種回答都可以,
1、$_SERVER['HTTP_REFERER']
可以獲取到仍和想頁面提交過來的頁面,
2、同樣你可以在A
B兩頁的表單中
附加一個隱藏的表單值
<input
type='hidden'
name='yemian'
value='A'>
兩個頁面分別使用不同的value,在獲取表單時
看下$_POST['yemian']的值來判斷提交的頁面
3、當然
,你同樣可以做兩個處理的php腳本,然後在兩個頁面分別調教到兩個不同的腳本中,這也就不需要在判斷了
