I´d like to make an auditing functionality in a PHP site. Then, I think a good best way is to get the IP address from the source and insert each action from this IP in my database.
I thought about the steps below:
- Get an IP address from the source and put this one in a session.
- Each button in the site, when clicked, does its job and inserts this IP and the button’s name and DateTime into the database.
Is this the best way?
So… as a result, for sample, I’d have a table like this:
+---+---------------+-------------+----------------------+ |Id | IP | Button | DateTime_Click | +---+---------------+------------------------------------+ |1 | 201.33.22.11 | Button_01 | 2020-06-15 10:03:28 | +---+---------------+------------+-----------------------+ |2 | 202.44.33.22 | Button_02 | 2020-06-15 11:25:35 | +---+---------------+------------------------------------+ |3 | 201.33.22.11 | Button_02 | 2020-06-15 12:36:28 | +---+---------------+------------------------------------+ |4 | 203.11.11.33 | Button_01 | 2020-06-15 12:45:24 | +---+---------------+------------------------------------+
I have some doubts because lots of IPs will access the php site and I need to insert into the database each click on the button from each IP source correctly.
How could I do it? Can I have some examples? Thanks so much! Best
Advertisement
Answer
$ip_address will get the ip the user is using. If you trust that the person being audited isn’t using a spoofed ip address, this should work for you:
$ip_address = $_SERVER['REMOTE_ADDR']; if (isset($_POST['Button_01'])) { // You didn't provide any SQL query in your question, so make sure your connection to the database is made then add your SQL INSERT. }