Simple PHP Help

  • Thread starter Thread starter LiE
  • Start date Start date

LiE

LiE

Caporegime
Joined
2 Aug 2005
Posts
26,245
Location
Milton Keynes
I'm trying to load content using PHP by using an include in my web page. I don't really know PHP but I've got this code which won't work.

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

the urls look like:

PHP:
			<ul id="menu">
		    <li><a href="index.php?page=home">Home</a></li>
		    <li><a href="index.php?page=aboutme">About Me</a></li>
		    <li><a href="index.php?page=prices">Prices</a></li>
		    <li><a href="index.php?page=services">Services</a></li>
		    <li><a href="index.php?page=promotions">Promotions</a></li>
		    <li><a href="index.php?page=contact">Contact</a></li>
			</ul>

Here's the error: http://www.gemmahayden.com/dev/v2/index.php?page=home

To me it looks like it's not translating the variable $page? Any help would be great thanks.
 
Thanks - works like a treat now. I knew it would be something simple :P
 
I took this example from a website, as I'm not familiar with PHP. Could someone provide me with a simple fix please? Thanks.
 
This is the code I'm using now:
PHP:
		<?php    
		$page = preg_replace('/[\W\.-]/si', '', $_GET['page']);
		    
		if (!isset($page))    
		{
		$page = "home";
		}
		include("include/$page.txt");
		    
		?>

The problem I have now is $page isset and therefore home isn't allocated by default. Can anyone help me?
 
I don't know php Ladforce, is there any chance you could amend my code? Thanks
 
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.
 
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.
 
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