I am trying to generate a dynamic file with php. Within this code, I need to declare some $_SESSION variables. For instance, the redirect url on refresh etc.
However, when I try to declare the $_SESSION variable and use fwrite, it fails to generate the php file.
How can I make sure that I can?
My code:
<?php
header('Content-Type: text/plain; charset=utf-8');
$file = fopen("testfile.php","w");
echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");
fclose($file);
?>
Thanks for your help
as apokryfos stated,
escaping the $_SESSION worked!
echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");
had to be modified to:
echo fputs($file,"\$_SESSION['test'] = 'Hello World. Testing!'");
Thanks!
Related
How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php
This may be simple to do, sorry if this is quite broad. I am fine with looking for any alternatives, but I would just like some help.
I need to be able to read variables from a file in PHP.
For instance, I would have a file like this:
$test = 10
$greeting = "Hello"
And PHP would be able to take these values and actually use them. The "code" above would be the entire file, add or take a few variables. The reason why I need to do it like this is so that other PHP files can also write variables to it. Is there any method that can do this, or something similar to this.
Thanks
Edit: This file would be a TXT file; it's not meant to be .php
Read lines from your text file:
<?php
$myfile = fopen("variables.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
//echo fgets($myfile) . "<br>";
// <Your code logic here.>
}
fclose($myfile);
?>
For each iteration of the while loop do whatever it is you need to do with the values read in from the text file.
I found a method. I'm still fairly new to PHP, so I'm using it quite strictly. I've decided to make a never ending PHP file, with all my variables in it:
<?php
//File name is variables.php
$test = 10;
$greeting = "Hello";
So then other PHP files can still write to it and other PHP files can read from it:
<?php
include "variables.php";
echo $greeting;
Output would be Hello.
Thank you for all your help everyone.
I want to print php variable in a text file.
Example :
<?php
$name='x' ;
?>
I want to put name value in text file for print it as hard copy .
Following is how to accomplish it.
<?php file_put_contents('filename', 'content');
Have a look at this function at PHP.net
Since your question does not include anything you have tried on your own (Which is generally recommended). Here is somewhere for you to start - the php file_put_contents function.
You can try something like this:
<?php
$file = 'some-file.txt';
$name = "x";
// Write the contents to the file
file_put_contents($file, $name);
?>
A few Google searches will surely provide some insight here as well.
Hope it helps!
How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php
I'm trying to send out a voucher, via the PHP's email() function, where I reference an external .php file using include() as the message contents. Heres the code:
$message = '<?php include ("/fullpath/inc/voucher.php"); ?>';
This isn't working because I'm declaring it as a text string, but if I don't it will just print out the contents and not put it as the content of the variable. Any ideas?
You can use the ob_buffer for that, try this:
<?php
ob_start(); // start buffer
include("file.php"); // read in buffer
$var=ob_get_contents(); // get buffer content
ob_end_clean(); // delete buffer content
echo $var; // use $var
?>
Example from http://www.webmaster-eye.de/include-in-Variable-umleiten.210.artikel.html (German)
In this post two solutions depending on the circumstances will be described..
voucher.php is returning a value
If voucher.php has a return statement in it you can safely use include to get a hold of this returned value.
See the below example:
voucher.php
<?php
$contents = "hello world!";
...
return $contents;
?>
...
$message = include ("/fullpath/inc/voucher.php");
voucher.php is printing data that I'd like to store in a variable
If voucher.php is printing data that you'd like to store in a variable you can use php's output buffering functions to store away the printed data and then assign it to $message.
ob_start ();
include ("/fullpath/inc/voucher.php");
$message = ob_get_contents ();
ob_end ();
ob_start();
include ("/fullpath/inc/voucher.php");
$message = ob_get_clean();
However output buffering in most cases is considered as bad practice and not recommended to use
Like in your case you better have a function declared in voucher.php that returns the message as a string. So all the code could be as simple as: $message = get_voucher();
I've encountered the same ploblem, here is the functions that resolves the problem for me. It uses the Tobiask answer
function call_plugin($target_) { // $target_ is the path to the .PHP file
ob_start(); // start buffer
include($target_); // read in buffer
$get_content = ob_get_contents(); // get buffer content
ob_end_clean();
return $get_content;
}
You want to use the readfile() function instead of include.
See: http://en.php.net/manual/en/function.readfile.php
If you want to get the =results= of PHP parsing the voucher.php one way is to make sure that you put voucher.php on a reachable URL, and then call it like:
$message = file_get_contents("http://host.domain.com/blabla/voucher.php");
This would insert the contents/output of the parsed voucher.php into the variable $message.
If you can't reach the voucher.php publically you have to rewrite it to output the string in the end with the return() function, or write a function in voucher.php that outputs the string you want when called after which you include() it and call the function from within this master PHP script.
$message = include '/fullpath/inc/voucher.php';
and withing /fullpath/inc/voucher.php
// create $string;
return $string;
$message=file_get_contents("/fullpath/inc/voucher.php");