Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

First directory only should determine include

Hello,

I use the following code to direct visitors to the correct page based on a friendly URL.

The problem is that sometimes visitors are taken to the wrong page.

For example:
      http://www.example.com/shop/page/sitemap/
ThAT URL displays the "Sitemap" instead of the "Shop" page.

Only the first directory should determine the page.



define("myBaseDir", dirname($_SERVER["PHP_SELF"]));
define("myURL", strtolower(str_ireplace(myBaseDir, '', $_SERVER["REQUEST_URI"])));
 
if (myURL=='/')
  {$page = 1; include_once('page.php');}
elseif (stristr(myURL, 'results'))
  include_once('results.php');
elseif (stristr(myURL, 'sitemap'))
  include_once('sitemap.php');
elseif (stristr(myURL, 'about'))
  include_once('about.php');
elseif (stristr(myURL, 'page'))
  include_once('page.php');
elseif (stristr(myURL, 'shop'))
  include_once('shop.php');
else
  include_once('404.php');

Open in new window

Avatar of ravenpl
ravenpl
Flag of Poland image

if (myURL=='/')
  {$page = 1; include_once('page.php');}
elseif (0 == strncmp(myURL, '/results/', 9))
  include_once('results.php');
elseif (0 == strncmp(myURL, '/sitemap/', 9))

and so on.
Refer http://pl.php.net/manual/en/function.strncmp.php and http://pl.php.net/manual/en/function.strncasecmp.php
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another example of a possible solution:
$sFirstDirectory = split("/", $_SERVER['PHP_SELF']);
$sFirstDirectory = strtolower($sFirstDirectory[1]);
  
switch($sFirstDirectory) {
	case "" : $page = 1; include_once('page.php'); break;
	case "results" : include_once('results.php'); break;
	case "sitemap" : include_once('sitemap.php'); break;
	case "about" : include_once('about.php'); break;
	case "page" : include_once('page.php'); break;
	case "shop" : include_once('shop.php'); break;
	default : include_once('404.php');
}

Open in new window