Simple include() problem

Reply

Join Date: Apr 2007
Posts: 45
Reputation: ReeciePoo is an unknown quantity at this point 
Solved Threads: 0
ReeciePoo's Avatar
ReeciePoo ReeciePoo is offline Offline
Light Poster

Simple include() problem

 
0
  #1
Jun 30th, 2007
  1.  
  2. <?php
  3. if (!$_GET['p']) {
  4. include "header.php";
  5. echo "<br/>";
  6. include "home.php";
  7. }
  8. if ($_GET['p'] && $_GET['p'] == "home" || $_GET['p'] == "pricing" || $_GET['p'] == "portfolio" || $_GET['p'] == "order" || $_GET['p'] == "contact") {
  9. include "header.php";
  10. echo "<br/>";
  11. include $_GET['p'].".php";
  12. }else {
  13. include "error404.php";
  14. }

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!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: Simple include() problem

 
0
  #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 Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: ReeciePoo is an unknown quantity at this point 
Solved Threads: 0
ReeciePoo's Avatar
ReeciePoo ReeciePoo is offline Offline
Light Poster

Re: Simple include() problem

 
0
  #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 Quick reply to this message  
Join Date: Nov 2003
Posts: 10
Reputation: 72dpi is an unknown quantity at this point 
Solved Threads: 0
72dpi's Avatar
72dpi 72dpi is offline Offline
Newbie Poster

Re: Simple include() problem

 
0
  #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.

  1.  
  2. <?
  3. $thefile = "inc/$page.php";
  4. if(isset($_GET['p'])){
  5. $id = $_GET['p'];
  6. }
  7. else{
  8. $id = 'home';
  9. }
  10. $page = $thefile;
  11. if(file_exists($page)){
  12. include $page;
  13. }
  14. else{
  15. include("inc/home.php");
  16. }
  17. ?>

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

  1. <?
  2. // keep people from accessing this page directly
  3. if (eregi('home.php', $_SERVER['PHP_SELF'])) { // go to index page
  4. header('Location: ../index.php?p=home');
  5. die(); // stop page from executing
  6. }
  7. ?>
  8. <p> </p>
  9. <h3>main Page</h3>
  10. <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:

  1. 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 Quick reply to this message  
Join Date: Jul 2007
Posts: 2
Reputation: whoosh is an unknown quantity at this point 
Solved Threads: 0
whoosh's Avatar
whoosh whoosh is offline Offline
Newbie Poster

Re: Simple include() problem

 
0
  #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..

  1. <?php
  2. if (!$_GET['p']) {
  3. include "header.php";
  4. echo "<br/>";
  5. include "home.php";
  6. exit;
  7. }
  8. if (($_GET['p']!=null) &&
  9. ($_GET['p'] == "home" || $_GET['p'] == "pricing" || $_GET['p'] == "portfolio" || $_GET['p'] == "order" || $_GET['p'] == "contact")) {
  10. include "header.php";
  11. echo "<br/>";
  12. include $_GET['p'].".php";
  13. }else {
  14. include "error404.php";
  15. }
  16. ?>

Another simple solution is given in the following code :
  1. <?php
  2. //Create an array with the valid page names
  3. $pagearray = array("home","pricing","portfolio","order","contact");
  4.  
  5. if (!$_GET['p'] || $_GET['p']==null) { // if p does not exist or p is null
  6. include "header.php";
  7. echo "<br/>";
  8. include "home.php";
  9. } elseif (in_array($_GET['p'],$pagearray)) { // else Check if the value of p is in the pagearray
  10. include "header.php";
  11. echo "<br/>";
  12. include $_GET['p'].".php";
  13. } else {
  14. include "error404.php";
  15. }
  16. ?>

regards.
Last edited by whoosh; Jul 3rd, 2007 at 8:35 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2003
Posts: 10
Reputation: 72dpi is an unknown quantity at this point 
Solved Threads: 0
72dpi's Avatar
72dpi 72dpi is offline Offline
Newbie Poster

Re: Simple include() problem

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

Re: Simple include() problem

 
0
  #7
Jul 8th, 2007
I do something very similar.

  1. /* set up valid pages in array */
  2. $array = array('contact','forum','about','cable');
  3.  
  4. /* if page isnt set, make default page=home.php */
  5. if(!isset($_GET['page']))
  6. $page = "home.php";
  7.  
  8. /* if it exists within the array, assign $page to $_GET['page']
  9. elseif(in_array($array, $_GET['page']))
  10.   $page = $_GET['page'];
  11.  
  12. /* if is is none of the above, generate error message */
  13. else
  14. die("error no page found");
  15.  
  16. /* if we get to this point, no error was found and we can go ahead and include the file from $page */
  17. include_once ('includes/' . $page . '.php');
  18.  
  19. }
Last edited by dr4g; Jul 8th, 2007 at 10:35 am.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC