Solved

variables from an outside text file

Posted on 2002-03-08
5
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

688 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