views:

70

answers:

3

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"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

<?php
echo curPageURL();
?>
+4  A: 

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.

Chetan
ok it worked it the view as $this->uri->segment(2);
ThomasReggi
A: 

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

ign
Sorry, didn't read you were using CI. Above comment should be helpful.
ign
+2  A: 

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.

DRL