[SOLVED] simple problem (easy fix for experienced)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
asi0917
Forum Commoner
Posts: 41
Joined: Thu Nov 25, 2004 10:37 am
Location: Shoreline, Washington
Contact:

[SOLVED] simple problem (easy fix for experienced)

Post by asi0917 »

<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>

i want to track the IPs that view my site,
how do i save $ip to a file like a .dat or something?
_________________
help=goodness
Last edited by asi0917 on Thu Nov 25, 2004 2:14 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you are must better off using a database. But if you insist on writing to a text file you can do either of the following

1) search these forums, it has been discussed several times.

[devnet]+writing +text +file[/devnet]

2) go to [php_man]fopen[/php_man], [php_man]fwrite[/php_man], [php_man]fclose[/php_man] to write to the file and [php_man]file_get_contents[/php_man] to read the file
Last edited by John Cartwright on Thu Nov 25, 2004 12:22 pm, edited 1 time in total.
PanK
Forum Commoner
Posts: 36
Joined: Mon Nov 22, 2004 1:24 pm
Location: Richmond Hill, ON, Canada

Post by PanK »

DO you have Database? IT would be easier to do.
Here is how to write to the file ...

Code: Select all

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (!fwrite($handle, $somecontent)) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    
    echo "Success, wrote ($somecontent) to file ($filename)";
    
    fclose($handle);
                    
} else {
    echo "The file $filename is not writable";
}
?>
Last edited by PanK on Thu Nov 25, 2004 12:22 pm, edited 1 time in total.
User avatar
asi0917
Forum Commoner
Posts: 41
Joined: Thu Nov 25, 2004 10:37 am
Location: Shoreline, Washington
Contact:

Post by asi0917 »

what does using a database intail?
crap, thats niffty, but more db info would be good
easy is music to my lazy ears
Last edited by asi0917 on Thu Nov 25, 2004 12:34 pm, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

you need a host that uses mysql
Post Reply