943,963 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1691
  • PHP RSS
Jun 30th, 2007
0

Simple include() problem

Expand Post »
PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ReeciePoo is offline Offline
45 posts
since Apr 2007
Jun 30th, 2007
0

Re: Simple include() problem

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.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Jun 30th, 2007
0

Re: Simple include() problem

could anyone please show me a quick resolution?
Reputation Points: 10
Solved Threads: 0
Light Poster
ReeciePoo is offline Offline
45 posts
since Apr 2007
Jul 3rd, 2007
0

Re: Simple include() problem

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.

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
72dpi is offline Offline
10 posts
since Nov 2003
Jul 3rd, 2007
0

Re: Simple include() problem

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 Syntax (Toggle Plain Text)
  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 :
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whoosh is offline Offline
2 posts
since Jul 2007
Jul 3rd, 2007
0

Re: Simple include() problem

Nice work whoosh.... =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
72dpi is offline Offline
10 posts
since Nov 2003
Jul 8th, 2007
0

Re: Simple include() problem

I do something very similar.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Mail not send
Next Thread in PHP Forum Timeline: parse error, unexpected T_STRING in





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC