Pregunta

On a page I am building, I require it to load a file into a portion of the page depending on a variable at the end of a URL. For example, if the page URL is test.php?page=lp then the page will include the file lp.php ...... now this was working for a while, but all of a sudden has stopped and I am just getting an error returned as though the file isn't there. I then 'prettied' up the script so that if the file "is not found" then a message saying so will be displayed, like this:

<?php $times = substr_count($_SERVER['PHP_SELF'],"/");
$rootaccess = "";
$i = 1;

while ($i < $times) {
 $rootaccess .= "../";
 $i++;
}
$y = date("Y");
$ymo = $y-1;

//$rootaccess.
if(!file_exists($page.".php")){
echo "The file requested (www.websitename.co.uk$rootaccess$page.php) was not found, or an error occured";

}
else {include($rootaccess.$page.".php");} ?>    

And all that happens is the result for if the file is not found is thrown up... so for the example of test.php?page=lp the message is returned "The file requested (www.websitename.co.uk/lp.php) was not found, or an error occured". But here's the thing, the file IS THERE, because the page also uses AJAX, and if I use the AJAX link to load up the exact same file at the exact same location, it works! (likewise if I enter the address show in the error, it works) I have used GET in order for the script to ascertain if $page has been set, as this changes other elements of the page too, so I know this is working... but yet for some reason it no longer wants to include the file - so what is the workaround for this? (I have tried it without the rootaccess bit as well, and it still fails) Thanks. (also, if anybody has any sort of idea as to why include($rootaccess.$page.".php") would work one day and then stop working the next then I'd love to know because at the moment I am just shouting at the page for it to work!!!)

¿Fue útil?

Solución

I think I have been stumbled the same issue before. Try to use:

$foo = trim($page.".php");

if(!file_exists($foo)) {
     :
} else {
     include(trim($rootaccess.$foo));
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top