4

I am making myself a webpage that simply redirects to one of a list of other websites upon page load. In order to be able to edit the list of sites more easily, I moved the list to a separate text document. Upon including the file in the PHP code, the browser reverts to parsing HTML and back again when the include is over. As a result, my code gets printed to the screen rather than executed. I am unable to implement PHP headers and tags within the text file, since that would defeat the purpose of keeping it external. As it stands now I have another script that allows me to append links to the file if I want to add them.

index.php, the file I am working with

    <?php 
        $array_content = array(include 'sites.txt');
        header("location: ".$array_content[rand(0,9)]);
    ?> 

sites.txt, the file I am including

    "http://www.nsa.gov/"
    ,"http://www.youtube.com/watch?v=oHg5SJYRHA0"
    ,"http://leekspin.com/"

Is there a way to just insert the text file into index.php, without printing it to the screen?

Jacob
  • 43
  • 1
  • 1
  • 4

4 Answers4

9

You should not!

Parse it as text (eg. by stripping the content of spaces and splitting it by commas) or, less preferably, make the content a PHP code and then include it using include.

Parsing TXT file as text

You should probably stick to parsing the file as text - you can use file() function for this. file() reads the whole file and returns its content as array of lines (with end of lines still attached). You can do whatever you need with such data (eg. strip it off the commas and spaces).

PHP code within included file

If you choose the former (including PHP code in TXT file, which is a bad idea if you want to make sites.txt available publicly), then you should try not to pollute global namespace. You can achieve it by using solution mentioned by other answers:

  • in sites.txt:

    <?php
    return 'your string goes here';
    
  • in your script:

    <?php
    $my_string = include('sites.txt');
    

The worst solution - eval()

Just including it for informative reasons. If you have a PHP code within your TXT file, you can use eval() for parsing it as PHP code. But remember - eval() is evil, avoid it at all costs.

zizozu
  • 501
  • 4
  • 9
  • 1
    @Phil: `eval()` does not require ` – zizozu Dec 06 '11 at 08:23
  • Ah, using file() worked great, and cleaned up my sites file quite nicely. Thanks! – Jacob Dec 07 '11 at 00:00
  • @Phil, also important to tell people what NOT to do and why. *Every* new PHP coder discovers eval() and is tempted by the dark side... –  Jul 15 '14 at 08:16
1

What you want is PHP's parse_ini_file() fn:
http://php.net/manual/en/function.parse-ini-file.php

You can easily then manage your list in a separate file and import it as an array for use without much fuss.

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • Yes, you should ;-). This is a better solution and much easier to secure. –  Jul 15 '14 at 08:21
  • not sure what examples you guys need above the link that I provide to the original documentation of the function... – Jakub Jul 31 '14 at 01:35
1

Suggestion:

Keep your "sites.txt" like this,

<?php 
$array_content = array( 
    "http://www.nsa.gov/",
    "http://www.youtube.com/watch?v=oHg5SJYRHA0",
    "http://leekspin.com/"
);
?>

and then, "index.php"

<?php 
    include('sites.txt');
    header("location: ".$array_content[rand(0,9)]);
?> 
Jim Jose
  • 1,319
  • 11
  • 17
1

You are mixing code with data. The txt file is just txt. What you need to do is store it in a plaintext format. Since there is only one field I'd recommend a flat file consisting of the sitenames separated by newlines.

Sitenames.txt:

site1.com
site2.org
site3.net

The php code to retrieve files:

$siteList = file('/path/to/Sitenames.txt');

Then you can perform any operations from the retrieved list.

Ravi
  • 727
  • 6
  • 19