•
•
•
•
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
![]() |
<?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
Location: North East Indiana
Posts: 491
Reputation:
Rep Power: 5
Solved Threads: 20
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.
<?
$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
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
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.
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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


Linear Mode