Page 1 of 1

include module, catching directory as file

Posted: Sat Jun 10, 2006 3:36 pm
by ozzy
Hey people. I have been trying to make a php include module type scriptwhich works from the $_get function. Everything works fine but is there any way of getting (see below) so that when i access ie. content.php?p=test directly it will redirect, or include content.php?p=test/index automatically, becouse currently if i acccess content.php?p=test it will be treated as a file, ie. content.php?p=test/.php. beocuse of the

Code: Select all

if(file_exists("content\\".$p.".php")){
and

Code: Select all

include "content\\".$p.".php";
Complete php:

Code: Select all

<?php

include 'config.php';

$p=$_GET["p"];

if(empty($p))
{
include "index.php";
//i made this include a file home.php if you are usign this for a site navigation then u want the url to b: site.com not site.com?p=home
} else {

if((strlen($p) > 20) or (strlen($p)  < 4) or (is_numeric($p))){
echo "$hackattempt";
} else {
if(file_exists("content\\".$p.".php")){
include 'includes/header.php';
include "content\\".$p.".php";
include 'includes/footer.php';
} else { echo"<center><b>404 Error</b><br /><br />The page you requested does not exist.</center>"; }
}
}

 ?>
I dont know how to solve this problem, thanks in advance!

Posted: Sat Jun 10, 2006 4:14 pm
by Christopher

Code: Select all

$filename = "content/$p.php";
if(file_exists($filename )){
    include 'includes/header.php';
    include $filename ;
    include 'includes/footer.php';
}

Posted: Sat Jun 10, 2006 4:54 pm
by ozzy
when i do that i get the following error:

Code: Select all

Parse error: syntax error, unexpected T_INCLUDE in C:\Program Files\xampp\htdocs\cos\content.php on line 20

Posted: Sat Jun 10, 2006 8:31 pm
by LiveFree

Code: Select all

$filename = "content/$p.php";
if(file_exists($filename )){
    include 'includes/header.php';
    include $filename;
    include 'includes/footer.php';
}

Posted: Sun Jun 11, 2006 3:02 am
by ozzy
I still suffer from the same problem. If i access "content.php?p=test" it will bring me to my 404. The only way of getting round this is to visit "content.php?p=test/index", but is there any way of getting "content.php?p=test" and keep my 404?

Code: Select all

<?php

include 'config.php';

$p=$_GET["p"];

if(empty($p))
{
include "index.php";
//i made this include a file home.php if you are usign this for a site navigation then u want the url to b: site.com not site.com?p=home
} else {

if((strlen($p) > 20) or (strlen($p)  < 4) or (is_numeric($p))){
echo "$hackattempt";
} else {
$filename = "content/$p.php";
if(file_exists($filename )){
    include 'includes/header.php';
    include $filename;
    include 'includes/footer.php';
} else { echo"<center><b>404 Error</b><br /><br />The page you requested does not exist.</center>"; }
}
}

 ?>

Posted: Sun Jun 11, 2006 5:34 am
by derchris
Why don't you just add another elseif,

for example something like this - not tested.

Code: Select all

<?php 

include 'config.php'; 

$p=$_GET["p"]; 

if(empty($p)) 
{ 
include "index.php"; 
//i made this include a file home.php if you are usign this for a site navigation then u want the url to b: site.com not site.com?p=home 
} else { 

if((strlen($p) > 20) or (strlen($p)  < 4) or (is_numeric($p))){ 
echo "$hackattempt"; 
} else { 
$filename = "content/$p.php"; 
if(file_exists($filename )){ 
    include 'includes/header.php'; 
    include $filename; 
    include 'includes/footer.php';
} elseif(file_ exists('content/$p/index.php')):
    include 'includes/header.php'; 
    include 'content/$p/index.php'; 
    include 'includes/footer.php';
} else { echo"<center><b>404 Error</b><br /><br />The page you requested does not exist.</center>"; } 
} 
} 

 ?>

Posted: Sun Jun 11, 2006 6:56 am
by ozzy
Ah, cool. Although i get the following error:

Code: Select all

Parse error: syntax error, unexpected T_STRING in C:\Program Files\xampp\htdocs\cos\index.php on line 21
Do you know how i would resolve this?

Posted: Sun Jun 11, 2006 8:23 am
by derchris
Try this:

Code: Select all

<?php 

include 'config.php'; 

$p=$_GET["p"]; 

if(empty($p)) 
{ 
include "index.php"; 
//i made this include a file home.php if you are usign this for a site navigation then u want the url to b: site.com not site.com?p=home 
} else { 

if((strlen($p) > 20) or (strlen($p)  < 4) or (is_numeric($p))){ 
echo "$hackattempt"; 
} else { 
$filename = "content/$p.php";
$filenamedir = "content/$p/index.php";
if(file_exists($filename )){ 
    include 'includes/header.php'; 
    include $filename; 
    include 'includes/footer.php'; 
} elseif(file_ exists($filenamedir)){ 
    include 'includes/header.php'; 
    include $filenamedir; 
    include 'includes/footer.php'; 
} else { echo"<center><b>404 Error</b><br /><br />The page you requested does not exist.</center>"; } 
} 
} 

?>

Posted: Sun Jun 11, 2006 8:46 am
by ozzy
Now i get...

Code: Select all

Parse error: syntax error, unexpected T_STRING in C:\Program Files\xampp\htdocs\cos\index.php on line 20

Posted: Sun Jun 11, 2006 8:50 am
by derchris
And now ?

Code: Select all

<?php 

include 'config.php'; 

$p=$_GET["p"]; 

if(empty($p)) 
{ 
include "index.php"; 
//i made this include a file home.php if you are usign this for a site navigation then u want the url to b: site.com not site.com?p=home 
} else { 

if((strlen($p) > 20) or (strlen($p)  < 4) or (is_numeric($p))){ 
echo "$hackattempt"; 
} else { 
$filename = 'content/$p.php'; 
$filenamedir = 'content/$p/index.php'; 
if(file_exists($filename )){ 
    include 'includes/header.php'; 
    include $filename; 
    include 'includes/footer.php'; 
} elseif(file_ exists($filenamedir)){ 
    include 'includes/header.php'; 
    include $filenamedir; 
    include 'includes/footer.php'; 
} else { echo"<center><b>404 Error</b><br /><br />The page you requested does not exist.</center>"; } 
} 
} 

?>

Posted: Sun Jun 11, 2006 9:01 am
by ozzy
I now get...

Code: Select all

Parse error: syntax error, unexpected T_STRING in C:\Program Files\xampp\htdocs\cos\index.php on line 20

Posted: Sun Jun 11, 2006 9:48 am
by derchris
Sorry, don't know.
I'm new to this whole PHP thing.

Posted: Sun Jun 11, 2006 4:52 pm
by ozzy
No problem. Thanks anyway. :)