View Full Version : Loading variables from a text file

12-01-2010, 04:30 AM
Hi Guys,

I am trying to load a series of variables from a text document for use within a web page. The text document shows them as a list, ie.

$variable_a=yes it works;
$variable_b=working also;
etc. etc.

From here I want to call the variables inside the main webpage. So I have tried pulling the variables from the text document using include but tht displays them at the top of the page in a single line. All I want is for them to be included in the php so that further down when I call the variable it displays "yes it works" as the result! I must usea txt file as it has a backend being edited by novices who I dont want to change this main page!

Any help is much appreciated!!

12-01-2010, 05:14 AM
Your pages must be .php (instead of .html), unless you specify that in an .htaccess file.

Below are some basic methods ... different ways to include text and PHP scripts ...

Copy and paste this into notepad, save it as "test.php" and upload it to your website.
See what happens ...


$variable_a="yes it works";
$variable_b="working also";

// or include a PHP script

// now, the normal HTML stuff
blah blah blah<br>

Does it work? <?=$variable_a?> <br>


This is more close to what you are attempting ... to include an external .txt file into your web page ...

Save some text in a file called "mytext.txt" ....
Now is the time for all good men to come to the aid of their country.

Then try this script ... call it "test2.php" ...

blah blah blah<br>

<?php include("mytext.txt");?>


Maybe you want to include some random lines of text from a remote text file.
Try this Javascript on your website:

<script type="text/javascript" src="http://www.catpin.com/ponder.php"></script>

(It's a random quote from my website)

The PHP script that is called, sends back the text using a Javascript content-type ...
Header("content-type: application/x-javascript");


12-01-2010, 05:52 AM
Take a look at this: http://us2.php.net/manual/en/function.file.php

It converts a plain text listing into an array. You may have to use trim() to remove the end of line characters and any end whitespace. It's about the best bet for using .txt the way you want.

12-01-2010, 11:24 AM
If it has to be a text file for security, how about parse_ini_file()?


Instead of having the variables specified as:

$variable_a = "blah";

just have:


12-01-2010, 01:02 PM
the extension is irrelevant unless you call as an absolute url ... e.g



echo $var1; // will work

// wont work

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum