0

Hello I'm currently working with php to generate a menu with a own build CMS system.

I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/"; Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:

as example =

$row_menu['page_link'] = page2;

$url . $row_menu['page_link'];

it will return as example : http://example.com/page2

But when I click again, it adds page2 again like : http://example.com/page2/page2

How do i prevent this?

Thanks in advance!

3 Answers3

0

Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.

Instead you can use HTTP_REFERER like

$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];

Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like

$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

REQUEST_URI will give you whatever comes after example.com, so leave that out all together.

$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];

You can find a full list of the $_SERVER references here.

Seunhaab
  • 550
  • 4
  • 6
0

Try this:

$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);

$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
    array_pop($uri_segments);
}

$uri = implode('/',$uri_segments);

$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];

echo $url;
Gabriel Moretti
  • 676
  • 8
  • 23
  • Thanks for your answer! But when i try this, the pages shows up blanc, i added a semicolon after ['page_link'], but that didn't solve the problem. – Remco Abalain Nov 05 '14 at 12:03