Jump to content

Requiring includes to come from server side


hybridpunk

Recommended Posts

Basically what I am trying to achieve is making sure any $page called originates from my server, but if no $page is defined, to include a default page I have set up. This is my code, and I cannot figure out what's wrong with it.

 

<?php
  $path = 'pages/';
  $extension = '.txt';

  if ( preg_match("#^[a-z0-9_]+$#i",$page) ){
    $filename = $path.$page.$extension;
    include($filename);
  }
if (!$page)
include 'pages/main.txt';
?>

 

An example URL I am using is www.mywebsite.com/index.php?$page=pages/tester

What am I doing wrong?

That would only happen if register_globals = On in the php.ini file, which it definitely should not.

 

You need to assign the value from the $_GET array to the variable you want to use. i.e. $page = $_GET['page'] or use $_GET['page'] directly.

Thank you very much, that fixed the problem. At first it wasn't working, then I realized that my urls were looking like index.php?page=pages/blah, after deleting pages/ from the url, everything worked fine. I greatly appreciate your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.