I have a variable that I want to send to another file to be echoed out to the screen. I tried sessions but no luck. I was searching and the searches say to use GET or POST but they aren’t working either. I don’t get anything when I try it, but then it could be wrong code. I have a hit counter that I am working on. The idea is to get the number from a file and then send that number to a different page to be seen. I want it like this so that no one else but me can see the numbers. Here is the hit counter that works. This will display the number to the screen if you make an index.php and an empty file called reader.txt.
$filename= “reader.txt” ;
$fd = fopen ($filename , “r”) or die (“Can’t open $filename”) ;
$fstring = fread ($fd , filesize ($filename)) ;
$fd = fopen ($filename , “w”) or die (“Can’t open $filename”) ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
I don’t know why the code is written this way, I have been following a tutorial. What I want to do is be able to use $fstring in another page. I want to be able to include this code in one page without it showing the results on that particular page. I think this makes sense.
well the simplest sollution I was able to figure out was to put the code that reads the file into the other page that I want to beable to use the variable. Then I just subtracted one from the count and all was good. But I want to know the real way to do it for future use. There has to be a better way.
< ?php $mf = "count.txt"; //filename $fh = fopen(mf, 'r+'); //file open count.txt and read with extra =write as well $counter = fread($fh, 5); // read the first 5 chars and assign it to counter $counter += 1; // counter = counter + 1 $stringData = "$counter"; // assing the new counter data as stringData fwrite($fh, $stringData); // write the stringData to the count.txt (overwrites) fclose($fh); //close the file echo $stringData; // echo the stringData value ?>
I can write via my contact script, but I can’t seem to open a file with this, maybe there is something wrong with my servers php implementation
this does work perfectly
< ?php $myFile = "testFile.txt"; // make sure the testFile.txt has file permission of 666 $fh1 = fopen($myFile, 'r'); $counter = fread($fh1, 5); $counter += 1; fclose($fh1); $fh2 = fopen($myFile, 'w'); fwrite($fh2, $counter); fclose($fh2); ?>
the to be read by you in another page
< ?php $myFile = "testFile.txt"; // make sure the testFile.txt has file permission of 666 $fh1 = fopen($myFile, 'r'); $counter = fread($fh1, 5); fclose($fh1); echo $counter; ?>