Code: Select all
<?php
$page = $_GET['page'];
if (stristr($page,'.') || stristr($page,'..') || stristr($page,'/') || stristr($page,'\\')) {
die('Hacking attempt was made!');
}
if ($page == '') {
$page = 'index';
}
include($page.'_left.inc');
?>
This is the correct code to do it. The syntax you where using was completely wrong.
Also it's better to use ' instead of " unless you want variables to be parsed in the string. Otherwise it slows things down because it makes the php have to process more even when there are no variables to display.
And it doesn't pass the variable straight to $page unless a certain variable is set in php.ini in the php configuration. Not only that but it's a security hazard the way you had it, and it won't work on all host setups. All of the variables in the url are stored in $_GET which is an array.
Also you should note that including is dangerous if you include using a variable. If somebody puts the name of a file you don't want them to execute or something else, they could damage your site. I added some checks at the top of the code that will prevent people from including files from other directories, but you will still need to make sure only approved files can be included otherwise they can screw around with your site.
Also you don't need to open and close php tags for every little bit of code you display. You only need to open it when you want to use php. And close it when you reach the end of the file or when you want to put some html. Also the closing tag is just "?>" not "php?>"
Anyway if you have any questions feel free to ask. And sorry for changing your code around so much, but I feel that teaching proper coding and secure coding is good.