• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

i wanna learn PHP

Hi,

Just put this code in main.php where you want the page included when the link (http://site.com/main.php?page=page.html) is clicked.

PHP:
<?
if ($_GET['page']) {
// add any action you want performed
include "$_GET[page]"; 
}
?>

For the above to work, page.html must be in the same directory, unless the filepath is included in $_GET[page]
(i.e ?page=/filepath/to/page.html)
 
That has huge security wholes, a better way to do that is:
PHP:
<?PHP
if (file_exists($_GET['page'])) {
// The file exists, so include...
include $_GET
} else {
// File doesn't exist, so output file does not exist...
echo 'File does not exist;
}
?>
 
Last edited:
I've always liked buying books when I wanted to learn something. Try going to Barnes and Noble and finding books on PHP. They have some books on PHP for about $10 at the store (I know I just bought one two days ago).
 
link92 said:
That has huge security wholes, a better way to do that is:
PHP:
<?PHP
if (file_exists($_GET['page'])) {
// The file exists, so include...
include $_GET
} else {
// File doesn't exist, so output file does not exist...
echo 'File does not exist;
}
?>

That has the exact same problem, no more secure than the first example :shame:

The correct way of doing this would be:


PHP:
<?
define('FILES_PATH', './files/');	// The path to your included files.
define('FILES_EXT', '.html');		// The extension to included files.

$g_pageName = baseName($_GET['page']);	// Make sure the path is safe.

if (empty($g_pageName))
{
	// No page entered, include the default page.
	require 'news/news.php';
}
else if (file_exists(FILES_PATH . $g_pageName . FILES_EXT))
{
	// A page was entered and it was found, include it.
	require FILES_PATH . $g_pageName . FILES_EXT;
}
else
{
	// A page was entered but not found, display an error.
	exit('The page you requested ("' . $_GET['page'] . '") could not be found!');
}
?>
 
Back
Top