Writing a PHP code with session variables in PHP - php

I cant able to write the session into a .php file
I can successfully echo out the session variable in this file, but cant save the output of that variable in the file i created "m3.php". Its displaying the php code only.
<?php
session_start();
echo $_SESSION["sub"];
$filename = "m3.php";
$ourFileName =$filename;
$ourFileHandle = fopen($ourFileName, 'w');
$written = "
<?php
echo \$_SESSION['sub'].\"<br>\";
?>
";
fwrite($ourFileHandle,$written);
fclose($ourFileHandle);
?>
This is the newly written file m3.php
<?php
echo \$_SESSION['sub'].\"<br>\";
?>
Thanks in Advance

Try this
$written = "<?php echo '{$_SESSION['sub']} <br>'; ?>";
Look at docs

Related

Bug fread in a txt file, read only one time

I'm creating a code to display the name of a server with enterprise rules, So for don't use Mysql i try a new things (for me) use php to read and rewrite files, that work perfectly for one part of my code and work perfectly but for the second he only read one time, and when i do a f5 the code don't increment.
He rewrite correctly because my file was at 000 and become 001
I try to use file() but he is disable since 7.0, try to use SplFileObject but it don't want to display anything and i don't like it because i understand nothing when i use it so i come back to fopen(),fread() and fwrite() and that don't work. I'm inPHP 7.3.1
The code that works :
<?php
if ( isset($_POST) AND !empty($_POST) ) {
$nom = "./config.txt";
$filez = fopen($nom, "r") or die("Unable to open file!");
$i = fread($filez,filesize($nom));
$year = getdate();
$idy = substr($year[year], 2);
$fichier = fopen("./resultsrv.txt", "w") or die("Unable to write file!");
for ($z; $z<$_POST['nbr']+1 ; $z++) {
$id = sprintf("%04d", $i+$z);
$nome = $_POST['type'].$_POST['OS'].$idy.$id."<br>" ;
echo $nome;
$nomewout = str_replace("<br>", ";", $nome);
fwrite($fichier,$nomewout);
}
$handle = fopen("./config.txt", "w") or die("Unable to write file!");
fwrite($handle,$id);
fclose($fichier);
fclose($handle);
}
?>
and the one that doesn't work because he doesn't increment :
<?php
if ( isset($_POST) AND !empty($_POST) ) {
$fileName = 'confchass.txt';
$read = fopen($fileName,"r");
$fn = fopen($fileName,"w+");
$i = fread($read,filesize($fileName));
$id = sprintf("%03d", $i+1);
echo "<div align='center'><h1>Le Chassis</h1>";
echo $_POST['Marque'].$_POST['DC'].$id;
echo "</div>";
fwrite($fn,$id);
fclose($read);
fclose($fn);
}
?>
I want he output a thing like XXXXXX001 and when i refresh or do a new POST from my forms he output XXXXXX002 and XXXXXX003 .... But he actualy output only XXXXXX001
The problem is that you open the file for reading and then for writing. But from the manual...
'w+' Open for reading and writing; place the file pointer at the
beginning of the file and truncate the file to zero length. If the
file does not exist, attempt to create it.
So this will blank out the file before you read the value from it.
To fix this (using your current method, you should read the value, then open it for writing and write the new value...
$read = fopen($fileName,"r");
$i = fread($read,filesize($fileName));
fclose($read);
$id = sprintf("%03d", $i+1);
echo "<div align='center'><h1>Le Chassis</h1>";
echo $id;
echo "</div>";
$fn = fopen($fileName,"w+");
fwrite($fn,$id);
fclose($fn);
You could shorten this by using file_get_contents() and file_put_contents().

Hide echo output, when text file is empty

I have a blog/vlog where I also share my listening habits from Spotify live.
I use a little snippet called Snip (available on Github) that fetches my Spotify stream, creates text files for my data (like track, artist, album and trackID)
Now I wanted to add a link on my webpage to the actual track I am playing using the variable $i for trackID
I've come up with this code thanks to some googling and tips/trix pages:
<?php
$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");
echo "<a href='https://open.spotify.com/track/";
echo fread($myfile,filesize("Snip_TrackId.txt"));
echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
fclose($myfile);
?>
This works just fine, but when I close down Spotify or pause, the text files empty themselves and my info box turns blank AND THAT'S THE WAY IT SHOULD BE :).
B
ut...... With my code above, the link, image and text "<- Listen" is still visible and I've been trying to figure out how I can fix this but I'm stuck.
(The link is also not really working as it only points to the part without the trackID)
So now I turn to you. Can you help me with a working solution for this?
If not, I will probably just keep things as they are.
you can just check the filesize before reading it:
<?php
if ((int)filesize("Snip_TrackId.txt") > 0)
{
$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");
echo "<a href='https://open.spotify.com/track/";
echo fread($myfile, filesize("Snip_TrackId.txt"));
echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
fclose($myfile);
}
Before writing a link check file for emptiness for example:
$file = "Snip_TrackId.txt";
if (0 < filesize($file)) {
$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");
echo "<a href='https://open.spotify.com/track/";
echo fread($myfile,filesize("Snip_TrackId.txt"));
echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
fclose($myfile);
}
Or if file contains spaces - check read data:
$myfile = fopen("Snip_TrackId.txt", "r") or die("Die!");
$data = trim(fread($myfile,filesize("Snip_TrackId.txt")));
if ($data) {
echo "<a href='https://open.spotify.com/track/";
echo $data;
echo "'target='_blank'><img width='18px' height='18px' title='Open with Spotify' src='np_spotify.png'><a/> <- Listen";
}
fclose($myfile);

Error on writing files in php

i have this php code:
<?php
echo ("Setting up data...");
$today = date("YmdHi");
$wtoday = $today
$im = $_GET["im"];
$fim = "tips/$today/im.txt";
$fwtoday = "tips/$today/today.txt";
?>
<?php
$fp = fopen ($fwtoday, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $wtoday);
fclose ($fp);
echo ("Today written");
}
else {
echo ("Today was not written");
}
?>
<?php
$fp = fopen ($fim, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $im);
fclose ($fp);
echo ("Im written");
}
else {
echo ("Im was not written");
}
?>
Finaly Today and Im was not written, where is my error ???
i dont think that have to do with file permissions.
i forgot to write about $fwtoday = "tips/$today/today.txt"; in the post, still not working.
Insert these lines to the front of your file, and share given errors:
error_reporting(E_ALL);
ini_set('display_errors','On');
$wtoday = $today
Missing semicolon, parse error.
That asice, you appear to be attempting to open a filename stored in the variable $fwtoday, which you don't seem to have defined anywhere.
I don't see the definition of $fwtoday. Also you don't need to do this:
?>
<?php

How to get text from txt file

I'm creating a html template with notification under nav bar , and admin can change that notification from the system the text of notification bar will be from notetxt file from the same location path where index.html is located i ave tried
<?php
foreach (glob("note.txt") as $filename) {
readfile($filename);
}
?>
and many other way but nothing happens it still stay blank
You are not echoing out the content of the textfile.
do it like this:
$myFile = "note.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
This will output your content of the file.
i'm using this code in pure html file
You can't use PHP functions in plain HTML file. MUST be written in a PHP file.
You have now in your code:
<span>
<!--?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?-->
</span>
Try with the examples above in a proper PHP file... then must work.
you can use file_get_contents function,
try something like this :
<?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?>
It's very simple use file_get_contents();
<?= file_exists('note.txt') ? file_get_contents("note.txt") : "file doesn't exists"; ?>
That is all what you need. file_get_contents() get the content of file and returns it. I've also checked if file exists because it may be your problem. Also make sure you have proper rights to read the file(CHMOD) and file is not empty.

edraw how to open file HttpOpenFileFromStream using php

edraw open file using HttpOpenFileFromStream in php
I am using this javascript code on load event
document.OA1.HttpInit();
document.OA1.HttpAddpostString("DocumentID", "sample5.docx");
document.OA1.HttpOpenFileFromStream("http://localhost/rte/action_open_file.php", "Word.Application");
php code action_open_file.php
header("http/1.1 200 OK");
$doc_file_name = $_REQUEST["DocumentID"];
$file_size = filesize($doc_file_name);
$file = fopen($doc_file_name,"r");
$data = fread($handle,$file_size);
fwrite($handle,$data);
fclose($handle);
But it opens empty file in web page but the sample5.docx has some text.
Your JS code is fine. Just replace your action_open_file.php code with the following:
<?php
$filename = $_REQUEST["DocumentID"];
echo "Get Stream Successfully!";
echo "EDA_STREAMBOUNDARY";
$fp=fopen("$filename","r");
print fread($fp,filesize("$filename"));
fclose($fp);
echo "EDA_STREAMBOUNDARY";
exit();
?>

Categories