Solved

variables from an outside text file

Posted on 2002-03-08
5
204 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

785 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