Hi I am trying to include a file which varies on which URL the user visits. Eg - if they visit articles.php?a=1 it will give them /articles/1.txt included, and for articles.php?a=hi, it will include /articles/hi.txt This is the code I am using, but it does not include the file, even though it exists. <?php $sArticleName = isset($_GET['a']) ? $_GET['a'] : 0 ; $sArticleFile = sprintf('%sarticles/%s.txt', $_SERVER['DOCUMENT_ROOT'], $sArticleName ); include('header.php'); if(file_exists($sArticleFile) && is_readable($sArticleFile)) { include($sArticleFile); } else { echo '<p>No articles found matching your criteria.</p>'; } include('footer.php'); ?> Code (php): Anyone any ideas what is wrong with it?
do it with a switch and case like switch($_GET['a']) { case "something": include('file'); break; case "eslething": include('fileother'); break; } ?> more explained here http://www.crivionweb.com/phpblog/php-tutorial-dynamic-pages-switch-and-case/
Thanks, but that's not what I mean. What I want is that the all I need to do is upload a txt file to the articles directory, and link to it. The script aint for me, and the person it is for doesnt want to have to edit a php file for every new page.
Looks fairly decent, var_dump($sArticleFile) for me and tell me the result, is this the correct folder where the articles are stored? Does using alternative means (i.e. file_get_contents) load the file correclty? Dan.
for security id use an array for allowed tags like so $allowed_includes = array("file1" => "file1.php","file2" => "file2.php"); if(isset($_GET['a'])){ if(in_array($_GET[a],$allowed_files)){ @include $allowed_types[$_GET['a']]; }else{ die('HACKING ATTEMPT!'); } } PHP: thats just a quick referance ... when including files by url make sure you look into the security well!