Dan382
Dan382

Reputation: 986

If URL contains 'foo' find and include a matching PHP include

I've written some code that includes 'home.php' if the URL contains 'index.php':

<?php if($_GET['page'] != "index.php") {
include('_includes/home.php');  } ?>  

This works fine for a specific page (in this instance the home page), but I want to extend this logic for any page on my site. For instance if the URL contained 'foo2.php' I'd want the the PHP to search for and include '_includes/foo2.php'.

I'm new to php so any help would be appreciated.

Upvotes: 0

Views: 97

Answers (2)

geomagas
geomagas

Reputation: 3280

How about:

if(isset($_GET['page'])) include "_includes/{$_GET['page']}.php";

Upvotes: 0

Daryl Gill
Daryl Gill

Reputation: 5524

if the URL was:

http://example.com/?page=foo2

if (empty($_GET['page'])){
  include ("_includes/index.php");
  exit;
}
$Page_Search = glob("_includes/*.php");

if (in_array($_GET['page'],$Page_Search)){
 include ("_includes/$page.'.php');
 exit;
}

This might be of use.

Upvotes: 1

Related Questions