Grow your CSS skills. Land your dream job.

sending variables

  • # August 14, 2009 at 1:15 am

    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.

    Code:
    $filename= “reader.txt” ;

    $fd = fopen ($filename , “r”) or die (“Can’t open $filename”) ;

    $fstring = fread ($fd , filesize ($filename)) ;

    echo “$fstring”;

    fclose($fd) ;

    $fd = fopen ($filename , “w”) or die (“Can’t open $filename”) ;

    $fcounted = $fstring + 1 ;

    $fout= fwrite ($fd , $fcounted ) ;

    fclose($fd) ;

    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.

    # August 14, 2009 at 2:41 am

    I will look into this when back from work…… I just checked out files in PHP the other day and have mine working the way I want it to

    # August 14, 2009 at 2:44 am

    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.

    # August 14, 2009 at 12:45 pm

    this should work…

    Code:
    < ?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

    edit:

    this does work perfectly

    Code:
    < ?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

    Code:
    < ?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;
    ?>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".