Log in

View Full Version : error include url



why not
07-22-2008, 05:11 AM
i have two problem with url in my code . Example :

<?PHP if (empty($_GET[page])) $_GET[page] = home; include "$svr_rootself/$_GET[page].php"; ?>
with this method my url is www.example.com/?page=example&ex=2&exe=3 . ( example=example.php in server root and worked 100% ) now when change manually url to ?page=example.php i have two error .



Warning: include(htdocs/site/example.php.php) [function.include]: failed to open stream: No such file or directory in htdocs\site\index.php on line 49

Warning: include() [function.include]: Failed opening 'htdocs/site/detail.php.php' for inclusion (include_path='.;\php\pear\') in site\index.php on line 49
how to fix this error ? regards

codeexploiter
07-22-2008, 05:25 AM
Try the below mentioned code for your purpose:

<?php
if (empty($_GET['page'])) {
$_GET['page'] = "home";
}
if(!strripos($_GET['page'],".php"))
include "$svr_rootself/$_GET['page'].php";
else
include "$svr_rootself/$_GET['page']";
?>

The error reported occurred for you because your PHP code appends a ".php" after the value stored in $_GET['page'] so if you pass "example.php" as its value then it will append ".php" after that and will result in "example.php.php".

The above furnished code checks whether it contains an extension if so it will not append ".php" on it.

I don't have any idea about the value stored in $svr_rootself variable and so it is difficult for me to tell whether it includes the correct path.

Hope this helps.

why not
07-22-2008, 07:16 AM
thanks ! but i try this code . send this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in \htdocs\site\index.php on line 54

why not
07-23-2008, 08:25 AM
any idea for fix this error ??

codeexploiter
07-23-2008, 11:45 AM
Plz try the following code:

<?php
$page = $_GET['page'];
if (empty($page)) {
$page = "home";
}
if(!strripos($page,".php"))
include "$svr_rootself/$page.php";
else
echo "$svr_rootself/$page";
?>

I've used a variable to hold the value of page name in this code. As I've mentioned I don't know the value stored in $svr_rootself variable make sure it contains correct value.

why not
07-23-2008, 01:51 PM
thanks ! this code worked WITHOUT ERROR But Show This Line Code htdocs/dir/browse.php just this line . my idea how to direct to any error page ! example : mysite.com/index.php?page=error ( this page not found ) or redirect to index.php or page=example thanks man , regards

motormichael12
07-25-2008, 12:40 AM
If you mean you want it to redirect to page=error if it doesn't exist, try this:


<?php
$page = $_GET['page'];
if (empty($page)) {
$page = "home";
}
if(!strripos($page,".php"))
include "$svr_rootself/$page.php";
else
header("Location: ./index.php?page=error&ref=$svr_rootself/$page");
?>

Then put this into your error.php file wherever you want it to show what page caused the error:

echo $_GET['ref'];

Optionally just use this to only redirect to an error page without telling the page that caused it:

<?php
$page = $_GET['page'];
if (empty($page)) {
$page = "home";
}
if(!strripos($page,".php"))
include "$svr_rootself/$page.php";
else
header("Location: ./index.php?page=error");
?>