title: A secure dynamic PHP includes?
body: <p>How do I create a dynamic PHP includes, that is secure? for example I have an index.php file with header.php and footer.php included inside of it and then grab other pages inbetween using </p>
  
  <pre><code>index.php?page=about
  </code></pre>
  
  <p>If possible it would need to be dynamic so using arrays and case would be time consuming and require modifications.</p>
  
  <p>I also want to be able to change the title of website as well depending on what page was included.</p>
  
  <p>I currently have this in place:</p>
  
  <pre><code>&lt;?php
  require_once 'includes/config.php';
  //Set values for page  
  $page_title = 'home page';
  $current_page = 'home';
  
  require_once 'includes/header.php';
  ?&gt;
  
  
  CONTENT
  
  
  &lt;?php
  require_once 'includes/footer.php';
  ?&gt;
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Would this be a safe way to include my pages?</p>
  
  <pre><code>if( isset( $_GET[ 'page' ] ) )
   {
     if( strpos( $_GET[ 'page' ], "/" ) )
       {
        $dir = substr( str_replace( ’’, ”, $_GET[ 'page' ] ), 0, strpos( $_GET[ 'page' ], "/" ) ) . "/";
  
        $file = substr( strrchr( $_GET['page' ], "/" ), 1 );
        if( file_exists( $dir.$file.".php" ) )
          {
           include( $dir.$file.".php" );
        } else {
           include( "home.php" );
        }
     } else {
        if( file_exists( basename( $_GET[ 'page' ] ).".php" ) ) 
          {
           include( basename( $_GET[ 'page' ] ).".php");
        } else {
           include( "404.php" );
        }
     }
  } else {
     include( "home.php" );
  } 
  </code></pre>