| PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >> |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread |
|
|
#1 (permalink) |
|
L'Awesome Advocate
Super #1
Joined in May 2004
Lives in .au
Hosted on Mango
2,423 posts
Gave thanks: 1
Thanked 5 times
|
Dynamic Includes
Hey guys, I'm not to confident with PHP but here's my shot at it.
Code:
<?php
if($x) {
if(file_exists("$x.php") {
include("$x.php");
} else {
include("error.php");
} elseif(!$x) {
include("main.php");
}
?>
__________________
When I get sad, I stop being sad, and be AWESOME instead. True story.
|
|
|
|
|
|
#5 (permalink) |
|
Registered User
Seasoned Poster
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
|
A few words about security (yes; I know I'm a nag).
Doing dynamic includes of this nature is very risky - particularly if you are also allowing your users to upload files elsewhere on the page. Consider you have a forum that allows users to upload an avatar or attach files to their posts and this script fails to adequately check the file type of the upload. Then a malicious user might create a file "exploit.jpg" that was actually a php file containing some malicious code. e.g. Code:
<?php echo "<strong>H4H D00D UR 73H L4M3R!!!11!!<strong>"; ?> The example I have given would just embarass you (and good grammar) by calling you 73H L4M3R; a real exploit might deliberately trash your site or overload the server disrupting your own service and others. It should be assumed that any user input into forms is unsafe. Your script could either check that the include in $x points to a directory with the appropriate permissions. Code:
<?php
if($x) {
if (strpos($x,"/mysafedir/")!=1) {
include("stophackingme.php");
}
else if(file_exists("$x.php") {
include("$x.php");
} else {
include("error.php");
} elseif(!$x) {
include("main.php");
}
?>
The most secure way to implement this is to know the pages you need before hand and use a method similar to that shown in this helpdesk howto: http://desk.surpasshosting.com/index...e1e564639e2e44 -wolf |
|
|
|
|
|
#6 (permalink) |
|
Registered User
Fresh Surpasser
Joined in Jul 2004
8 posts
Gave thanks: 0
Thanked 0 times
|
Wolfy, all I can say is WOW! Thanks for the fantastic info! Your so right about using using include or require. I totally see it now. (My light bulb just dimly lit!)
Thank you! ~VH~ |
|
|
|
|
|
#7 (permalink) |
|
Surpass Fan
Excelling Contributor
Joined in Jan 2004
Lives in Clinton, Massachusetts
Hosted on Serenity x.x.40.51
994 posts
Gave thanks: 0
Thanked 0 times
|
Actually, that's still dangerous. I could put in index.php?x=../file.php and open file.php in the parent directory. index.php?x=../../filephp and the same goes for the parent's parent directory.
I suggest using a switch. It's the safest way to do what you want. For each page that you have to add a new line just like all the others, with the correct name and file. PHP Code:
|
|
|
|
|
|
#9 (permalink) |
|
Registered User
Seasoned Poster
Joined in Jul 2004
31 posts
Gave thanks: 0
Thanked 0 times
|
In my defense ... I did say in my original post that the method I had just proposed was insufficient for precisely the reason you posit and linked to a helpdesk article which used the case method.
-wolf |
|
|
|