Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    log file system question.

    I created a code in which each time a user login successfully, php will write to a text file the activity of the user (login). But I can't figure it out on how it will create a new text file to write once the size of the text file exceeded 100 kb.

    Here is the piece of code.

    PHP Code:

    // insert to log file
        
        
    $datasizefilesize("logs.txt");
        if (
    $datasize <= 100)
        
        {
        
    $user$_POST['username'];
    $timendategmdate("l dS \of F Y h:i:s A");

    $data" User $user login at $timendate \r\n";
    $file fopen("logs.txt","a");
     
    fwrite($file,"$data ");
    fclose($file);    
        } 
    I can add something like this
    PHP Code:
    if ($datasize >= 100)
        {
        
    $datasizefilesize("log2.txt");
        
    $user$_POST['username'];
    $timendategmdate("l dS \of F Y h:i:s A");
    if (
    $datasize <= 100) {
    $data" User $user login at $timendate \r\n";
    $file fopen("log2.txt","a");
     
    fwrite($file,"$data ");
    fclose($file);    
    }
        } 
    But what if log2 exceeded 100 kb. It's not appropriate to create another condition for log3.txt. I think looping is the solution, in which you will also increment a number and concatenate it with text file name. But I don't know where to start any ideas? Thanks
    Last edited by Anishgiri; 07-01-2010 at 10:05 AM.

  • #2
    New Coder
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    If I'm honest that I would never do is have a file created that holds the user's activity unless it's stored outside of the public folder so it can't be access from the web and only you can access it.

    Have you considered storing this type of information into a database?

  • #3
    Regular Coder
    Join Date
    May 2010
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply, I decided to use session to address my problem. Is there any risk with that?

    Sorry for a noob question, but how can you make a web page not accesible from the web?
    Use the include path?

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    How are you using a session for that? You're still writing a text file?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #5
    Regular Coder
    Join Date
    May 2010
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I still write on a text file. Why? Is there a risk?

  • #6
    New Coder
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    I would say yes to that because anyone could access that and know more than they should, yet this is only user's activity but still it is your duty to keep users information private even activity. It's one of those things every webmaster should do myself I've always handled users information in a secure manner either SSL or securing it somewhere out of reach from the public hands.

    But as you asked how to access a file outside of the public folder, well this is simple but requires a little bit of work.

    Now if your using cPanel then it would be before the "public_html" folder (unless that has changed, haven't used cPanel for a few years now.) For Plesk it would be before "httpdocs" or "httpsdocs" (SSL Only).

    Now for including such files that reside outside of the public folders as most people use PHP to conger this. By simply adding this as soon as you start a PHP file:

    PHP Code:
    <?php

    require_once($_SERVER['DOCUMENT_ROOT'] . '/../thefile.txt');

    // Rest of code goes here

    ?>
    Remember to add more /../ to it if it's further above the 'public_html' or 'httpdocs' folder so for example if I placed 'thefile.txt' into a subdomain (http://subdomain.example.org/) you would then do this:

    PHP Code:
    <?php

    require_once($_SERVER['DOCUMENT_ROOT'] . '/../../../../private/thefile.txt');

    // Rest of code goes here

    ?>
    As said above you just keep adding more '/../' the higher you go up in folders this should then include the data no matter what. So hopefully this answers your question...Need anymore help then just ask!
    Last edited by bennyy007; 07-02-2010 at 12:32 PM.

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Untested, but it should accomplish what you're looking for:
    PHP Code:
    /* - - - - - - - - - -
     * The log file
     */

    $log_file './logs.txt';

    /* - - - - - - - - - -
     * The data to be logged
     */

    $log_data 'This is to be logged.';

    /* - - - - - - - - - -
     * Does the log file already exist?
     */

    $log_exists is_file$log_file );

    /* - - - - - - - - - -
     * Log the data
     * If the file already exists, append it
     */

    file_put_contents(
        
    $log_file,
        ( 
    $log_exists "\n" '' ) . $log_data,
        ( 
    $log_exists FILE_APPEND )
    );

    /* - - - - - - - - - -
     * If the filesize is over 100kb, rename it
     * - This will loop logs1.txt, logs2.txt, logs3.txt, etc,
     *   till it comes to a file name that does not exist
     */

    if ( filesize$log_file ) > 102400 )
    {
        
    $inc 1;
        do {
            
    $log_file_bak './logs' $inc++ . '.txt';
        } while( 
    is_file$log_file_bak ) );
        
    rename$log_file$log_file_bak );

    Last edited by kbluhm; 07-02-2010 at 04:39 PM.

  • #8
    Regular Coder
    Join Date
    May 2010
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for the late reply.

    @Benny thanks for this usefull information.


    @kbluhm, thanks for the help, I will try your code. I did try something, but I think your code is more effeficient so I will try that. Below is the solution that I did.


    PHP Code:

    // the value of counter is from a mysql fetch array

    $_SESSION['increment'] = $info['counter'];

     if (!
    file_exists("log1.txt"))
         
         {
             
                 
    mysql_query("UPDATE user_table SET counter= 1  ")
     or die(
    mysql_error());
         }


    // insert to log file

         
        
    $a $_SESSION['increment'];
        
    $filename"log" .$a ."." ."txt";
        
    $file fopen("$filename","a");
        
    $datasizefilesize("$filename");

        
    $user$_POST['username'];
    $timendategmdate("l dS \of F Y h:i:s A");

    $data" User $user login at $timendate \r\n";
     
    fwrite($file,"$data ");
    fclose($file);    
        

    if (
    $datasize >= 100000)
        {
            
             
    mysql_query("UPDATE user_table SET counter = counter + 1 ")
        or die(
    mysql_error()); 
        
        } 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •