User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,864 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,626 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1033 | Replies: 6
Reply
Join Date: Apr 2007
Posts: 45
Reputation: ReeciePoo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReeciePoo's Avatar
ReeciePoo ReeciePoo is offline Offline
Light Poster

Question Simple include() problem

  #1  
Jun 30th, 2007
 
<?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['p'] 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
(o(o.(o.O).O)O) - Shhhhh They're Watchin Us!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: North East Indiana
Posts: 491
Reputation: Puckdropper is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 20
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: Simple include() problem

  #2  
Jun 30th, 2007
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.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote  
Join Date: Apr 2007
Posts: 45
Reputation: ReeciePoo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReeciePoo's Avatar
ReeciePoo ReeciePoo is offline Offline
Light Poster

Re: Simple include() problem

  #3  
Jun 30th, 2007
could anyone please show me a quick resolution?
(o(o.(o.O).O)O) - Shhhhh They're Watchin Us!!!
Reply With Quote  
Join Date: Nov 2003
Location: Perth WA
Posts: 8
Reputation: 72dpi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
72dpi's Avatar
72dpi 72dpi is offline Offline
Newbie Poster

Re: Simple include() problem

  #4  
Jul 3rd, 2007
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...
www.72dpi.net.au _root.ofallevil
Reply With Quote  
Join Date: Jul 2007
Posts: 2
Reputation: whoosh is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
whoosh's Avatar
whoosh whoosh is offline Offline
Newbie Poster

Re: Simple include() problem

  #5  
Jul 3rd, 2007
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['p]. Also it is a good practice to validate the $_GET['p'] 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.
Last edited by whoosh : Jul 3rd, 2007 at 8:35 am.
Reply With Quote  
Join Date: Nov 2003
Location: Perth WA
Posts: 8
Reputation: 72dpi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
72dpi's Avatar
72dpi 72dpi is offline Offline
Newbie Poster

Re: Simple include() problem

  #6  
Jul 3rd, 2007
Nice work whoosh.... =)
www.72dpi.net.au _root.ofallevil
Reply With Quote  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Simple include() problem

  #7  
Jul 8th, 2007
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');
 
}
Last edited by dr4g : Jul 8th, 2007 at 10:35 am.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC