variables from an outside text file

Hi...

Is it possible to set a list of variables, in for example a text file called info.txt, and that I later can refer to those variables inside my PHP file.

I don't have access to a mySql base so I wondered if there is a "file solution" like I've explained above.

(I know that there is a way to return a number of characters from a file, with starting point and an ending point)
reidarjAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
Yes that's possible

use the include() or require() statement. But you shouldn't use the txt extension unless your webserver is set up to let PHP parse these files.

Let's say you have a standard setup. THen I would call this file variables.php

Like this

<?
$var1 = 20;
$var2 = 30;
?>

Then you could include this file in other files like this:

include("variables.php");
0
 
BatalfCommented:
Or you could create a function which parses through text-files like you suggested:

$fh = fopen("info.txt","r");
$data = fread($fh,filesize($fh));
fclose($fh);

Then you could "parse" the file

// An array of each line
$lineArray = split("\n",$data);

// Loop through each line
for($no=0;$no<count($lineArray);$no++)
{
    // If each line is like this
    // variable1 = 20
    $tempArray = split("=",$lineArray[$no]);
    // first argument in array is the name of
    // the variable, the other one the value
    //
    // Create a variable with the name of the first
    // argument in array and initialize it with the
    // value of the second element
    $$tempArray[0] = $tempArray[1];
}

0
 
dkjariwalaCommented:
Hello,

There are two ways to accomplish this.

1. PHP provies a very useful function called parse_ini_file

So if you have say following ini file

-----------------------------------------------------------------------------
;sample.ini

; This is a sample configuration file
; Comments start with ';',

myservername = this_is_my_server
port = 2323
demostring = demostring
-----------------------------------------------------------------------------

Now you can use following code to read,

<?php

$settings_array = parse_ini_file('sample.ini');

print "Value of myservername is " . $settings_array['myservername'];
print "Value of port is " . $settings_array['port'];
print "Value of demo string is ". $settings_array['demostring'];

?>

Cool isnt it ?
But here there is one drawback,
If you have not protected your ini file, then anyone can request it from browser and see all the values as it would be passed as plain text.


2. In that case it would be better to use following approach which I do with all my scripts.

You may have 2 files,
config.php , app.php

config.php may look like,

<?php
//config.php

$config_arr['myservername'] = 'This is my server';
$config_arr['port'] = 2323;
$config_arr['demostring'] = 'demostring';

?>

and app.php would look like

<?php
//app.php

require 'config.php' ; //assuming config.php and app.php are in same directory.
global $config_arr;

print "Value of myservername is " . $config_arr['myservername'];
print "Value of port is " . $config_arr['port'];
print "Value of demo string is ". $config_arr['demostring'];

?>

This method is safer, cause even if some one requests file config.php, say by

http://yourserver.com/config.php

it won't output anything and your information is protected !!! :)

in case of ini files, you would have to protect them somehow cause

http://yourserver.com/sample.ini would reveall all the text in INI.

I hope this *answers* your question,
Regards,
JD
0
 
reidarjAuthor Commented:
All I wanted was a simple solution... Batalf provided all the information I needed... Thanks
0
 
dkjariwalaCommented:
I guess you should have mentioned that you want simple solution !! ;)

I told you about possible ways with pros/cons about theM,
Regards,
JD
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.