Solved

variables from an outside text file

Posted on 2002-03-08
5
187 Views
Last Modified: 2008-03-06
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)
0
Comment
Question by:reidarj
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 100 total points
ID: 6850575
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
 
LVL 32

Expert Comment

by:Batalf
ID: 6850586
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
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6852424
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
 

Author Comment

by:reidarj
ID: 6855694
All I wanted was a simple solution... Batalf provided all the information I needed... Thanks
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6855733
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now