include module, catching directory as file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

include module, catching directory as file

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$filename = "content/$p.php";
if(file_exists($filename )){
    include 'includes/header.php';
    include $filename ;
    include 'includes/footer.php';
}
(#10850)
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Code: Select all

$filename = "content/$p.php";
if(file_exists($filename )){
    include 'includes/header.php';
    include $filename;
    include 'includes/footer.php';
}
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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>"; }
}
}

 ?>
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post 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>"; } 
} 
} 

 ?>
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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?
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post 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>"; } 
} 
} 

?>
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post 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>"; } 
} 
} 

?>
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

Sorry, don't know.
I'm new to this whole PHP thing.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

No problem. Thanks anyway. :)
Post Reply