rob.m rob.m - 2 months ago 10
PHP Question

How to read SERVER['REQUEST_URI full dynamic path?

The code below works well when we are at a url path

/sard/
e.g.
www.example.com/sard/
yet if my url goes to
www.example.com/sard/page2
it breaks.

<?php
$twitter = '';
switch ($_SERVER['REQUEST_URI']) {
case '/sard/':
$twitter = 'example';
break;
case '/pie/':
$twitter = 'example_2';
break;
default:
break;
}
?>


How can I make it so that it always read the
uri
full dynamic path?

Answer

You can use strpos.

if(strpos($_SERVER['REQUEST_URI'], '/sard') === 0){
    echo 'oke';
}

This will match all the urls who start with /sard. Because the position of /sard is at 0.

Comments