-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLOGdownload.php
46 lines (33 loc) · 1.91 KB
/
LOGdownload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
//load the database configuration file
include_once 'LOGdbconfig.php';
//execute query that selects all records from the user_logs table, ordered by the id field in ascending order
$query = $db->query("SELECT * FROM user_logs ORDER BY id ASC");
//if there are any records in the result set, the code in the following block will be executed
if($query->num_rows > 0){
//sets the delimiter used to separate values in the CSV file to a comma
$delimiter = ",";
//set the filename of the csv file to be generated, using the current date in the format yyyy-mm-dd
$filename = "user-logs_" . date('Y-m-d') . ".csv";
//creates a file pointer $f that points to a temporary memory location where the csv file will be written
$f = fopen('php://memory', 'w');
//create array containing the column headers for the csv file
$fields = array('ID', 'EMAIL', 'URL', 'IP ADDRESS', 'BROWSER', 'LOGIN ATTEMPT');
//write the field names of the data to the file as a csv row
fputcsv($f, $fields, $delimiter);
//iterates over the rows of data returned by a database query, formats each row as a csv row, and writes it to the file
while($row = $query->fetch_assoc()){
$lineData = array($row['id'], $row['email'], $row['page_url'], $row['user_ip_address'], $row['user_agent'], $row['created']);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//after the data has been written to the file, the script moves the file pointer back to the beginning of the file,
//sets the http response headers to force the browser to download the file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
exit;
?>