Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.