<?php
   if (!$_GET['p']) {
  include "header.php";
  echo "<br/>"; 
  include "home.php"; 
 } 
 if ($_GET['p'] && $_GET['p'] == "home" || $_GET['p'] == "pricing" || $_GET['p'] == "portfolio" || $_GET['p'] == "order" || $_GET['p'] == "contact") { 
  include "header.php";
  echo "<br/>";
  include $_GET['p'].".php";
 }else {
  include "error404.php";
 }

The above code simply does the obvious index.php?p=pagenamehere

and when $_GET is non existant simply just includes home.php

when i do index.php?p=home it works fine
but when just index.php

the error404.php is included at the bottom and obviously i dont want that.

any help would be appreciated

please excuse the quality of my script im only just getting back into php

thankyou
Reece

Recommended Answers

All 6 Replies

That's how it's supposed to work.

When you call the page with index.php, the &p= part is NULL. The null (or unset) case is not handled in your if statement, so it's branching to else.

could anyone please show me a quick resolution?

hey Reecie, it's as Puck says, since it is not defined, it shows your 404.

if you want to make life easier, I would specify a folder
for inclusion files.
I call my folder "inc" and place all files in there.

<?
$thefile = "inc/$page.php"; 
if(isset($_GET['p'])){
$id = $_GET['p'];
}
else{
$id = 'home';
}
$page = $thefile;
if(file_exists($page)){
include $page;
}
else{
include("inc/home.php"); 
}
?>

If you want to secure the include files, add some php at the top:

<? 
// keep people from accessing this page directly 
if (eregi('home.php', $_SERVER['PHP_SELF'])) {   // go to index page 
header('Location: ../index.php?p=home'); 
die();  // stop page from executing
} 
?>
<p> </p>
<h3>main Page</h3>
<p><strong>This is your page text!</strong></p>

I would create a .htaccess file & add that into your root directory.

In it, you would simply add the following directive:

ErrorDocument 404 /index.php?p=error404

(and make sure your error404.php is in the inc folder)

Sorry if this isn't exactly what you were chasing, but may be a better way to do what you are trying...

Hi,
72dpi has given a good solution.

But if you want your same code to work .. add an "exit", just before closing the if loop.
That means, your code after the first if loop will not be executed if there is no value for $_GET. Also it is a good practice to validate the $_GET against null. Another correction made is the proper seperation of the conditions inside the second if loop.
Hope it helps

See the corrected code below..

<?php
   if (!$_GET['p']) {
  include "header.php";
  echo "<br/>"; 
  include "home.php"; 
  exit;
 } 
 if (($_GET['p']!=null) && 
 		($_GET['p'] == "home" || $_GET['p'] == "pricing" || $_GET['p'] == "portfolio" || $_GET['p'] == "order" || $_GET['p'] == "contact")) { 
  include "header.php";
  echo "<br/>";
  include $_GET['p'].".php";
 }else {
  include "error404.php";
 }
 ?>

Another simple solution is given in the following code :

<?php
//Create an array with the valid page names
$pagearray = array("home","pricing","portfolio","order","contact");

if (!$_GET['p'] || $_GET['p']==null) { // if p does not exist or p is null
	include "header.php";
	echo "<br/>";
	include "home.php";
} elseif (in_array($_GET['p'],$pagearray)) { // else Check if the value of p is in the pagearray
	include "header.php";
	echo "<br/>";
	include $_GET['p'].".php";
} else {
	include "error404.php";
}
?>

regards.

Nice work whoosh.... =)

I do something very similar.

/* set up valid pages in array */
$array = array('contact','forum','about','cable');
 
/* if page isnt set, make default page=home.php */
if(!isset($_GET['page']))
   $page = "home.php";
 
/* if it exists within the array, assign $page to $_GET['page']
elseif(in_array($array, $_GET['page']))
   $page = $_GET['page'];
 
/* if is is none of the above, generate error message */
else
    die("error no page found");
 
/* if we get to this point, no error was found and we can go ahead and include the file from $page */
include_once ('includes/' . $page . '.php');
 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.