Jump to content

Include file name


peter_anderson

Recommended Posts

Hey,

 

Can anyone help me with this little script?

 

I've written a script for a client, but they want an additional part added.

 

What they want is a file (eg index.php?) and if visited with a subpart to the URL (eg index.php?a=123) it would load a file called 123 in the directory.

 

They are new to coding and don't want to have to add a new file with all the formatting.

 

So, it is sort of like php include, but it needs to take the ?a=NAME from the url and add the NAME.txt file.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/141968-include-file-name/
Share on other sites

<?php
$file = isset($_GET['a'])?basename($_GET['a']):index;

if (!file_exists($file)) 
     $file = "index";

include($file . ".php");
?>

 

Should do it. Use caution though, I took steps in there to prevent someone from loading a different file, but I am not guaranteeing that at all, I would prefer to use an array of defined files that are allowed and if $file is in that array then include it. This is an example of that:

<?php
$file = isset($_GET['a'])?basename($_GET['a']):index;
$allowed = array("index", "home", "contact");

if (!file_exists($file)) 
     $file = "index";
elseif (!in_array($file, $allowed))
     $file = "index";

include($file . ".php");
?>

 

That should be the safer version.

Link to comment
https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743359
Share on other sites

Well, I tried it, but I can't get it working :(

 

I'm more used to ASP and HTML, so excuse all my problems.

 

The file name should be "news.php" and accessing it by "news.php?a=1" should bring up "1.txt".

 

Any ideas how to best do it?

 

<?php

$file = isset($_GET['a'])?basename($_GET['a']):index;
$allowed = array("index", "home", "contact");

if (!file_exists($file)) 
     $file = "index";
elseif (!in_array($file, $allowed))
     "Sorry but there are no files of that name to view at this time. Please check back later.";

include($file . ".php");
?>

 

Is the code I used.

 

But, it tries to download the script.

 

Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743386
Share on other sites


<?php

$file = isset($_GET['a'])?basename($_GET['a']):index;
$allowed = array("index", "home", "contact");

if (!file_exists($file)) 
     $file = "index";
elseif (!in_array($file, $allowed))
      $file = "index"; // this line is here so we show something, it defaults to index if there is a problem. You also did not use the "echo" command for the "Sorry... so it was an invalid deal.

include($file . ".php");
?>

 

See comments above.

Link to comment
https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743388
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.