簡體   English   中英

如何從PHP的URL中提取信息?

[英]How do I pull information from URL in PHP?

可以說url是http://example.com/product/3 ,我只想檢索http://example.com/product/之后的內容。 我發現了這一點,它與域相呼應,但是我如何得到這三個域。 其這種方法可靠嗎? 我也在使用codeIgniter。

<?php
function curPageURL() {
$pageURL = 'http';
$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;
}
?>

<?php
echo curPageURL();
?>

使用$this->uri->segment(n) 它的文檔在這里:

http://codeigniter.com/user_guide/libraries/uri.html

對於您的代碼,您將使用:

$curPageURL = $this->url->segment(2)

編輯:修復了代碼中的錯誤。

是的,使用codeigniter API的URI類。

base_url的細分為零,因此,在您的情況下,乘積將為細分1,id為2。

$product_id = $this->uri->segment(2)

正如chetan所說,這在用戶指南中有明確記錄。

您應該改用parse_url: http : //php.net/manual/es/function.parse-url.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM