Jump to content

[SOLVED] include file based on variable in url


denoteone

Recommended Posts

I want to inlcude a .txt file based on a varibale passed in the url and if that variable is not set in the url then show the default include file.

This is what I have so far but I am pretty sure that I am not checking if it is set properly.

The links could look like so.

 

http://www.mysite.com?page=project1  this would include the txt file named project1.txt

or

http://www.mysite.com  //this would show the default

 

<?PHP


if($_GET['page'] isset){

$page = $_GET['page'] . '.txt';
include $page;
}else{
include 'default.txt';
}

?>

Link to comment
Share on other sites

To avoid raw php code inclusion from a remote hacker's site, if you are going to include a file this way, either validate that $_GET['page'] only has very specific values in it that match your pages or check if the file $_GET['page'].'.txt' exists on your web server, or make sure that allow_url_include is off (php5.2.0 and higher), or make sure that allow_url_fopen is off (before php5.2.0.)

 

Without these checks, someone can visit your page using  http://www.mysite.com?page=http://hackers_site/project1 where he has a file project1.txt on his site that contains raw php code that will then get included and executed on your server.

Link to comment
Share on other sites

Thanks Little Guy. Is there a reason behind not using "include" ?

 

Hey love the avatar they make me smile too!

 

 

I think the reason behind this, is because its not .php

if it was .php, u can assum its html/php code in the file

so include can process the code in the file

fread/echo doesnt process the code. it just spits it out to the browser as is.

 

and if its a php script, and ya name it .txt its a bad idea

as than anyone can see the script, and look for ways of breaking the script

 

I think u can replace the fread/echo with readfile instead :)

 

Link to comment
Share on other sites

Thanks Little Guy. Is there a reason behind not using "include" ?

 

Hey love the avatar they make me smile too!

 

 

I think the reason behind this, is because its not .php

if it was .php, u can assum its html/php code in the file

so include can process the code in the file

fread/echo doesnt process the code. it just spits it out to the browser as is.

 

and if its a php script, and ya name it .txt its a bad idea

as than anyone can see the script, and look for ways of breaking the script

 

I think u can replace the fread/echo with readfile instead :)

 

 

In my opinion allowing users to choose a file and execute it is a little more dangerous with include/require...

Link to comment
Share on other sites

[quote]To avoid raw php code inclusion from a remote hacker's site, if you are going to include a file this way, either validate that $_GET['page'] only has very specific values in it[/quote]

So if all my text files that I am including will only have a file name of 4 characters would the following code test work?

if(isset($_GET['page'])) {

 

$pagecount = strlen($page);

                if($pagecount > 4){

                                          this is not a valid file

                }else{

                                          $page = $_GET['page'].'.txt';

                                          include $page;

}else {

  include 'default.txt';

}

[/code]

Link to comment
Share on other sites

this is a safer way than your method. This way, they can only choose a page that you have allowed explicitly:

<?php
$allowed_pages = array("project1", "main", "project2", "project3");
if (isset($_GET['page']) && in_array($_GET['page'], $allowed_pages)){
$page = $_GET['page'].".txt";
}
else{
$page = "main.txt";
}
include($page);
?>

Link to comment
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.