| | |
Simple include() problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
PHP Syntax (Toggle Plain Text)
<?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!!!
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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.
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.
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.
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.
If you want to secure the include files, add some php at the top:
I would create a .htaccess file & add that into your root directory.
In it, you would simply add the following directive:
(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...
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)
<? $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:
PHP Syntax (Toggle Plain Text)
<? // 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:
PHP Syntax (Toggle Plain Text)
ErrorDocument 404 /index.php?p=error404
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
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..
Another simple solution is given in the following code :
regards.
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)
<?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 Syntax (Toggle Plain Text)
<?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.
I do something very similar.
PHP Syntax (Toggle Plain Text)
/* 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
![]() |
Similar Threads
- Read/write to same file > once, Help (C++)
- include(); problem (PHP)
- include file problem (C++)
Other Threads in the PHP Forum
- Previous Thread: Mail not send
- Next Thread: parse error, unexpected T_STRING in
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





