473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to create a log file in PHP?

65
Hi all,

Can anyone give me some suggestions on the log file that can keep track the user login and logout, edit data and delete data in the system.

Currently i have not idea to create the page and dunno how to start to create it..

Can some one guide me?

Thanks in advance.
Nov 2 '07 #1
5 19981
Lumpy
69
Hi all,

Can anyone give me some suggestions on the log file that can keep track the user login and logout, edit data and delete data in the system.

Currently i have not idea to create the page and dunno how to start to create it..

Can some one guide me?

Thanks in advance.
The php manual has a whole section on filesystem functions which can be found at http://php.net/filesystem

And also there is a decent tutorial that will kind of show you how to get files open and edited at http://www.tizag.com/phpT/files.php.

I would start with these resources and go from there. Good Luck!!
Nov 2 '07 #2
wish
65
Hi,

Here is the example code from website. I try it but it cant work?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $ac_arr = file('/path/to/copy/access_log');
  3. $astring = join("", $ac_arr);
  4. $astring = preg_replace("/(\n|\r|\t)/", "", $astring);
  5. $records = preg_split("/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $astring, -1, PREG_SPLIT_DELIM_CAPTURE);
  6.  
  7. $sizerecs = sizeof($records);
  8.  
  9. // now split into records
  10. $i = 1;
  11. $each_rec = 0;
  12. while($i<$sizerecs) {
  13.   $ip = $records[$i];
  14.   $all = $records[$i+1];
  15.  
  16.   // parse other fields
  17.   preg_match("/\[(.+)\]/", $all, $match);
  18.   $access_time = $match[1];
  19.   $all = str_replace($match[1], "", $all);
  20.  
  21.   preg_match("/\"GET (.[^\"]+)/", $all, $match);
  22.   $http = $match[1];
  23.   $link = explode(" ", $http);
  24.   $all = str_replace("\"GET $match[1]\"", "", $all);
  25.  
  26.   preg_match("/([0-9]{3})/", $all, $match);
  27.   $success_code = $match[1];
  28.   $all = str_replace($match[1], "", $all);
  29.  
  30.   preg_match("/\"(.[^\"]+)/", $all, $match);
  31.   $ref = $match[1];
  32.   $all = str_replace("\"$match[1]\"", "", $all);
  33.  
  34.   preg_match("/\"(.[^\"]+)/", $all, $match);
  35.   $browser = $match[1];
  36.   $all = str_replace("\"$match[1]\"", "", $all);
  37.  
  38.   preg_match("/([0-9]+\b)/", $all, $match);
  39.   $bytes = $match[1];
  40.   $all = str_replace($match[1], "", $all);
  41.  
  42.   print("<br>IP: $ip<br>Access Time: $access_time<br>Page: $link[0]
  43.     <br>Type: $link[1]<br>Success Code: $success_code
  44.     <br>Bytes Transferred: $bytes<br>Referer: $ref
  45.     <br>Browser: $browser<hr>");
  46.  
  47.   // advance to next record
  48.   $i = $i + 2;
  49.   $each_rec++;
  50. }
  51. ?>
  52.  
Can anyone guide me? I already change the file path..var/log/httpd/access_log.. but still cant work..

thanks in advance.
Nov 2 '07 #3
Markus
6,050 Expert 4TB
Do you get an error?
Nov 2 '07 #4
pimmy
9
I am not sure what you are trying to do, but logging to a file is as simple as that:
Expand|Select|Wrap|Line Numbers
  1. $message = 'Something happened in my script..';
  2. $timestamp = date('d/m/Y H:i:s');
  3. $log_file = '/var/log/my_script.log';
  4.  
  5. error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);
  6.  
Make sure that the directory where you are directing your log exists. PHP will not give you any error message, but just not create any log.

Cheers,

Pimmy
Nov 2 '07 #5
wish
65
Hi,

I try it.

My log file is actually is keep one record in linux..in location /var/log/http/access_log

My previous code is split the access log file into more meaning report come out.

Is it correct for the code to retrieve the access_log file?
Nov 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nicholas Then | last post by:
I am writing an ASP.NET application and I have a class that I have written to create a vCard...it just returns a string with all the necessarry info... Anyway...is there a way that I can create a...
1
by: egsys | last post by:
I am simply trying to create a file in access 2003. Receive an error message that: "Workgroup administrator couldn't create the workgroup information file. Make sure that you have specified a...
4
by: Bnob | last post by:
It is possible to create a file in local user-machine? With this code in code-behind: fs = New System.IO.FileStream(lFileCSV, System.IO.FileMode.Create, System.IO.FileAccess.Write) The file...
11
by: Russ Green | last post by:
I need help writing a method to create a simple text file. I want that file to be saved to a local drive (client side of my asp.net app). The filename will always be the same "license.txt" but the...
1
by: Victor | last post by:
Hi, We have following setup here Web Server (Win2000) and FTP Server (Win 2000) We have ASP.NET application which runs on Web Server and writes file(s) onto FTP Server. We created asp.net...
3
by: christiang | last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting service, you pass an url to its cgi and you get a thumbnail of that url. Of course they don't have the screenshots of all the...
1
by: j7o0s5 | last post by:
How to create file help chm?
2
by: SteMc | last post by:
Hi, I'm trying to create a text file on an asp page but am having loads of trouble. At the minute what happens is this - I have a form with a textarea. When the form posts, the action is to run...
0
Kosal
by: Kosal | last post by:
Hi I have problem with vb.net I want to create file setup or exe file for run on Client Computer that no need to install vb.net 2005 but I never do so I don't know what can i do? Please help...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.