Solved

incompatible variable with file_get_contents

Posted on 2007-12-03
23
283 Views
Last Modified: 2013-12-13
Seem like file_get_contents does not work with variable.

I created two files.
I want trying to use the variable from the VariableInsideFile.txt file but I don't want to use the include function.

//FIILE 1     VariableInsideFile.txt
   $Mokey

//File 2 Do_VariableInsideFile.php
<?php

$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
echo $CallOtherVariable;
?>

The result should be
Can't equal to happy"

but this is what i got with my script.
$Money

if I write the whole script withing using file_get_contents by using the include and use the variable directly on the same page then I don't see any problem.

Like below

<?php
$Money="Can't equal to happy";
$CallOtherVariable="$Money";
echo $CallOtherVariable;
?>

So my question is how to use the variable inside the txt file.



0
Comment
Question by:jetleestye
  • 11
  • 6
  • 5
  • +1
23 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20398806
This is a bad idea, IMO, but you can use the eval() function to interpret the file input.  Make sure that file is protected against unauthorized edits!

http://us2.php.net/manual/en/function.eval.php
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 100 total points
ID: 20398856
I think this is what you are looking for :)
<?php
 

$Money="Can't equal to happy";

$CallOtherVariable=file_get_contents("VariableInsideFile.txt");

eval("echo $CallOtherVariable;");
 

?>

Open in new window

0
 

Author Comment

by:jetleestye
ID: 20398896
How come the variable can't be assign in the eval function.
syntax error, unexpected

<?php
$Money="Can't equal to happy";
 
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
eval("$Money= $CallOtherVariable;");
echo $Money;
 
?>
0
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 100 total points
ID: 20398955
Well, consider what you are doing in that last bit of code. $CallOtherVariable will contain the string '$Money' after you call 'file_get_contents("VariableInsideFile.txt").

So if your method worked, this line:

eval("$Money= $CallOtherVariable;");

Would be evaluated to:

 $Money= $Money

That wouldn't make any sense.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 20398967
If you explain more fully your intentions, perhaps we can help you devise a better method that doesn't require the use of a messy eval() call.
0
 

Author Comment

by:jetleestye
ID: 20399021
I am trying to build a multilingual page.
The .txt file will help translator to translate more easily than reading the messy codes.
0
 

Author Comment

by:jetleestye
ID: 20399050
If I write my script like this

<?php
$Money="Can't equal to happy";
 
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
eval("$Get_Money= $CallOtherVariable;");
echo $Get_Money;
?>
Notice: Undefined variable: Get_Money in on line 5
Parse error: syntax error, unexpected '=' in  : eval()'d code on line 1
Notice: Undefined variable: Get_Money
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20399056
See below.

I'm with glcummins.  This is a clumsy and dangerous method.  If anyone is able to edit that file, they could run just about any code they wanted.  Explain your need and strategy, and perhaps we can come up with a better way for this to get done.


<?

//inside money.php

$money="my money";

?>
 

<?

$t = file_get_contents('money.php');

/* at this point, $t="<?\n//inside money.php\n\$money=\"my money\";\n?>"
 

echo $t,'-',$money,'-';

/* output:

<?

//inside money.php

$money="my money";

?>--

*/
 

eval($t);

echo $t,'-',$money,'-';

/* output:

<?

//inside money.php

$money="my money";

?>-my money-

*/

?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
ID: 20399068
How about something like this:
<?php
 

$language = 'english';
 

include ('languages/$language.php');
 

echo $Money;
 

?>
 

The file 'languages/english.php' contains:
 

<?php
 

$Money = "Can't equal to happy.";
 

?>
 

Each additional language just has a place for the translators to edit the necessary variables. No code will be included in the language files other than simple variables and text.
 

?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
ID: 20399099
It may also be helpful to examine how larger projects (such as osCommerce) have already solved this problem. osCommerce uses a method very similar to what I described above, but the individual language files define constants instead of variables:

From 'languages/english/index.php':
  define('HEADING_TITLE', 'Let\'s See What We Have Here');

  define('TABLE_HEADING_IMAGE', '');

  define('TABLE_HEADING_MODEL', 'Model');

  define('TABLE_HEADING_PRODUCTS', 'Product Name');

  define('TABLE_HEADING_MANUFACTURER', 'Manufacturer');

  define('TABLE_HEADING_QUANTITY', 'Quantity');

  define('TABLE_HEADING_PRICE', 'Price');

  define('TABLE_HEADING_WEIGHT', 'Weight');

  define('TABLE_HEADING_BUY_NOW', 'Buy Now');

Open in new window

0
 

Author Comment

by:jetleestye
ID: 20399280

I still like my idea better but as you mension about the risk of being attack on the code.
I am kinda scared.

So  as long as we can figure out on how to turn the text into variable then I think .

The script that you wrote problaly will not work on my script.

For example.
This is your $CurryTry tried  tried out of  $MaximumAllow.
The script should output like this.
This is your 1st tried out of  9.



0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 24

Expert Comment

by:glcummins
ID: 20399318
Sure, that would still work. main file, you will have:

echo "This is your $CurryTry tried out of $MaximumAllow.";

Then in your language file:
<?php
 

$CurryTried = "1st";

$MaximumAllow = "9";
 

?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
ID: 20399325
Although in this example, $CurryTry and $MaximumAllow look like dynamic values that you wouldn't really define in a semi-static language file. Independent language files are more suited to values that will not change.
0
 

Author Comment

by:jetleestye
ID: 20399450
seem like your script is not working, it still output the whole thing.

I rewrite the code  but I having a problem
<?
 $money="my money";

$t = file_get_contents('VariableInsideFile.txt');

echo "$t";

/* output:
echo "<br> Seem like nothing display below here"
eval($t);
echo $t,'-',$money,'-';
/* output:

*/
?>
0
 

Author Comment

by:jetleestye
ID: 20399492
Ths is the clear looking , but

<?php
 
$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
$Testme= eval("echo $CallOtherVariable;");
//    I TESTED THE $Testme, the $Testme doesn't holding any value in it.
if(empty($Testme)){echo "eval can't turn into Variable ";}
 
?>
0
 

Author Comment

by:jetleestye
ID: 20399506
and even this too

<?php
 
$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
$Testme= eval("$CallOtherVariable;");
//    I TESTED THE $Testme, the $Testme doesn't holding any value in it.
if(empty($Testme)){echo "eval can't turn into Variable ";}
 
?>
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20399738
One more try with full commenting.  Remember that:

1) money.php is a separate PHP file on your server.  The ONLY thing in the file for this example is:
    <?
    $money="my money";
    ?>

2) eval() takes the parameter you pass it, and attempts to parse it as PHP.  It will return TRUE on success, FALSE on failure, unless you have used the return() statement in the code.  All actions executed as a result of the parsed eval() code will impact the current scope of execution.

3) "Include ('money.php');" will effectively do the same thing as "$t=get_file_contents('money.php');eval($t);", but with less risk.

4) "DECLARE (my_money,'my money');" will effectively do the same thing as "$t=get_file_contents('money.php');eval($t);" but with less risk, and 'my_money' will be available as a constant, not a regular variable.


<?

// REMEMBER: money.php is a separate file!

/*

  At the start, the following are true:

    $t === undefined/null

    $money === undefined/null

    $z === undefined/null

*/
 

$t = get_file_contents('money.php');

/*

  At this point, the following are true:

     $t === (string) "<?\n\$money=\"my money\";\n?>"

     $money === undefined/null

     $z === undefined/null

*/
 

echo "$t-$money-";

/*

  The previous line will echo this:
 

   <?

   $money="my money";

   ?>--
 

  Note the two dashes are printed at the end because $money is still undefined.  You should also see

  an error in your PHP log regarding that fact.

*/
 

$z = eval($t);

/*

  At this point, the following should be true:  

     $t === (string) "<?\n\$money=\"my money\";\n?>"

     $money === (string) "my money"

     $z === (boolean) true
 

  If PHP could not parse the string in $t, or if another error occurred, $z will be false.

*/
 

echo "$t-$money-";

/*

  The previous line should output the following:
 

     <?

     $money="my money";

     ?>-my money-

*/

?>

Open in new window

0
 

Author Comment

by:jetleestye
ID: 20400192
It looks complicated to follow, let me try.
I think it would be easier if you just write what is in first page and what is in the second page.
Le me try
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20400210
Just five lines of code, including the line in money.php.  The idea is that whatever you send through eval() acts exactly like PHP code, just as if you had typed it there.  If you were to remove the eval() line, and copy/paste the contents of money.php into its place, you would have the same execution.

The concept itself is not difficult, but I think your strategy for approaching this is making it unnecessarily so.  Why do you feel it is necessary to have eval() execute the contents of what could easily be an include file?
0
 

Author Comment

by:jetleestye
ID: 20400257
again, I am trying to build  a multilingual page using text file.
I got your point, I can't get the eval work like copy and paste.
Let me do more research on the eval thing here

0
 

Author Comment

by:jetleestye
ID: 20400267
this would be a better example on how to code my script.
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo "$str.<br>";
eval("\$str = \"$str\";");
echo $str. "\n";
?>
0
 

Author Comment

by:jetleestye
ID: 20400293
I got it
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 20400311
Yes!  Exactly!

Still, you're going about it the hard way.  There's no reason to push all this through eval().  Using glcummins' previous input, you can use a series of constant declarations.  The new code would look like this:


<?

// LANG-EN.PHP

declare('coffee_string', "This is a cup with my coffee in it.");

// however many other declarations you have

?>
 

<?

switch ($language) {

  case 'FR': require ('lang-fr.php'); break;

  case 'DE': require("lang-de.php'); break;

  case 'EN':

  default: require("lang-en.php"); break;

}

echo coffee_string;

?>

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

22 Experts available now in Live!

Get 1:1 Help Now