Simple PHP Help

  • Thread starter Thread starter LiE
  • Start date Start date
This is what I have now. It sets the default correctly if I navigate to index.php.

PHP:
		<?php    
		$page = preg_replace('/[\W\.-]/si', '', $_GET['page']);
		    
		if (!isset($_GET['page']))    
		{
		$page = "home";
		}
		include("include/$page.txt");
		    
		?>

Added in the suggestion by philjohn but every page returned the 404.php as the include.

When you added his suggestion did you change the folder in his example "files" to "include"?
 
Also are you editing all of this directly on your official website's webspace?

if so this aint a good idea, you'd be better setting up a personal webserver such as xampp and messing around with your code on your pc, where only you have access to it and if any problems arise no errors will appear to the public. Once you have completed your task you can then copy the new files to the public webspace

I'm adding changes to the web server yes, but in the dev/v2/ directory.

When you added his suggestion did you change the folder in his example "files" to "include"?

Yes.
 
PHP:
$a = preg_replace('/\W/si', '', $_GET['a']);  
$filename = "$a.php";
if (file_exists($filename)) {
include $filename;
} else {
include 'welcome.php';
}

This is simple, protected and, well, it works...
 
sorry for the noob question

I fully understand how the above all works, but how do I implement it

E.g I have a template index page

I have a file called includes, that has all the different text files for pages etc

So on my index file do I simply need a hyperlink akin to this

<a href="index.php?page=home" title="Gemma Hayden Home">Home</a>

Changing the above to whatever I need

Therefore upon clicking the link the link uses the file

Index.php grabs the href variable and chucks out the text file stated in the link?
 
sorry for the noob question

I fully understand how the above all works, but how do I implement it

E.g I have a template index page

I have a file called includes, that has all the different text files for pages etc

So on my index file do I simply need a hyperlink akin to this

<a href="index.php?page=home" title="Gemma Hayden Home">Home</a>

Changing the above to whatever I need

Therefore upon clicking the link the link uses the file

Index.php grabs the href variable and chucks out the text file stated in the link?

Yep, but all you need is <a href="?page=xx">, don't need to use index.php :).
 
This is what I have now. It sets the default correctly if I navigate to index.php.

PHP:
		<?php    
		$page = preg_replace('/[\W\.-]/si', '', $_GET['page']);
		    
		if (!isset($_GET['page']))    
		{
		$page = "home";
		}
		include("include/$page.txt");
		    
		?>

Added in the suggestion by philjohn but every page returned the 404.php as the include.

This should work:

PHP:
$base = './includes';

if (!isset($_GET['page']) || empty($_GET['page'])    
{
	$file = "$base/home.php";
}
else
{
	$page = preg_replace('/[\W\.-]/si', '', $_GET['page']);
	$file = "$base/$page.php";
}

$file = file_exists($file) ? $file : "$base/404.php";
require $file;

Also, don't use .txt as your extension for PHP includes; it allows people to see their raw contents, since they won't be executed by PHP before being served.
 
Also, don't use .txt as your extension for PHP includes; it allows people to see their raw contents, since they won't be executed by PHP before being served.

I typically use .inc.php - they will be executed but you can still see that they're included files by the addition of the .inc
 
I typically use .inc.php - they will be executed but you can still see that they're included files by the addition of the .inc

Personally I find the whole '.inc' paradigm completely unnecessary. Within the framework I use (and virtually every other framework for that matter), every file is included except the front-end dispatcher (usually index.php). Even if you have multiple front-end files, it's usually fairly obvious which PHP files are included and which aren't just by their location in the directory structure.
 
Last edited:
This should work:

PHP:
$base = './includes';

if (!isset($_GET['page']) || empty($_GET['page'])    
{
	$file = "$base/home.php";
}
else
{
	$page = preg_replace('/[\W\.-]/si', '', $_GET['page']);
	$file = "$base/$page.php";
}

$file = file_exists($file) ? $file : "$base/404.php";
require $file;

Also, don't use .txt as your extension for PHP includes; it allows people to see their raw contents, since they won't be executed by PHP before being served.

Thanks I'll make the changes and let you know how I get on.
 
Back
Top Bottom