开发者

How do I pull information from URL in PHP?

Lets say the url is http://example.com/product/3 and I only want to retrieve what is after http://example.com/product/. I found this, it echos the domain but how do I get the three. Its this method reliable? I'm using codeIgniter also.

<?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_S开发者_JS百科ERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

<?php
echo curPageURL();
?>


Use $this->uri->segment(n). Documentation for it is here:

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

For your code, you would use:

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

Edited: Fixed a bug in the code.


Yes, use the URI class of the codeigniter API.

The base_url is segment zero, so in your case, products would be segment 1 and the id segment 2.

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

As chetan mentioned this is clearly documented in the user guide.


You should use parse_url instead: http://php.net/manual/es/function.parse-url.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜