So recently I saw a this website: https://0day.rocks/ and there is a text about this server being a Tor node. But under that is a number of bytes (terabytes) that server has transferred since last reset. I already saw similar thing on other websites but I'm really interested in how to do this in php (if it is even possible). And just to say I'm not asking you to write code for me, I just want someone to tell me what to read or where to get started, etc. Thanks in advance.
-
I don't write this as an answer, since you asked specifically for php, but you may want to log this at the webserver level (e.g. apache2) so you don't have to implement such counter in each individual PHP file. See this https://serverfault.com/questions/14613/how-can-i-see-how-much-bandwidth-each-apache-virtual-host-is-using for additional information. – Fitzi Jan 31 '19 at 18:01
-
I think that you solved it :D I'm not looking strictly for php solutions so you can write it as an answer. – Filip Timko Jan 31 '19 at 18:07
2 Answers
Like discussed in the comments, you are not strictly looking for a php solution, therefore I'd recommend logging incoming and outgoing traffic on the webserver level.
Apache for example already comes which such abilities. Simply enable mod_status ( https://httpd.apache.org/docs/2.4/mod/mod_status.html ) which amongst others provides the following information:
- The status of each worker [...] and the total number of bytes served by the worker
- A total number of accesses and byte count served
- Averages giving the number of requests per second, the number of bytes served per second and the average number of bytes per request
It also seems to be possible to get the bytes sent from apaches access log. See this: https://serverfault.com/questions/14613/how-can-i-see-how-much-bandwidth-each-apache-virtual-host-is-using
Other webservers like NGINX, Tomcat, etc. should have similar capabilites.

- 1,641
- 11
- 17
On Linux it's easy to query interface statistics which will count all bytes transferred, and not just those by Tor, or Apache.
The stats from 0day are being collected directly from the Tor controller, which keeps track of it's own traffic usage.
If your public interface is eth0
, you can get the bytes transferred and received from the following files:
/sys/class/net/eth0/statistics/tx_bytes
/sys/class/net/eth0/statistics/rx_bytes
With systemd predictable network interface naming, the interface may look more like enp3s0f0
.
Alternatively you can parse info out of /proc/net/dev
but the first two are easier.
In PHP, any user can read those using file_get_contents
to display on a page. You can use an answer like this to convert the bytes in another unit if you want.
The kernel documentation about the net statistics can be found here: https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net-statistics

- 68,777
- 11
- 134
- 162