Skip to content

Instantly share code, notes, and snippets.

@yeungon
Created February 10, 2018 06:11
Show Gist options
  • Save yeungon/2c5fdc8cca9e2d6a583543ca6ec1fd7a to your computer and use it in GitHub Desktop.
Save yeungon/2c5fdc8cca9e2d6a583543ca6ec1fd7a to your computer and use it in GitHub Desktop.
a simply PHP query log_currently used at tudien.net
<?
/*
* @author: Vuong Nguyen [email protected]
* fopen() => mở
* fget() => đọc S, C,
* fread() => đọc hết file theo dung lượng, lấy dung lượng theo filesize()
* fwrite() => viết ==> thuộc tính W: ghi đè, A: ghi kế tiếp, nếu xuống dòng *\r\n
* fclose() => đóng
*/
/*mở file, tạo file nếu chưa có và ghi append tiếp tục a*/
$p = fopen("a.txt", 'a');
/*ghi vào file từ $word = _REQUEST[]*/
/*https://stackoverflow.com/questions/1483497/how-to-put-string-in-array-split-by-new-line
lưu ý
*/
if ($word!=NULL) {
fwrite($p, ", ".$word);
}
fclose($p);
/*fgets() => đọc file theo string.
* fgetc() => đó thể đọc theo kí tự đầu
/*mở file cơ chế đọc*/
$file = "a.txt";
$p = fopen($file, 'r');
/*
* fread(2 đối số), đọc theo dung lượng file, có thể dùng hàm filesize() để lấy dung lượng hàm.
**/
$read = fread($p, filesize($file));
// echo $read;
// echo "<br>";
// $toarray = preg_split("/\r\n|\n|\r/", $read);
$toarray = explode(',', $read);
/*array_slice($array, $offset [, $lenght, $reoder]);
* offset là vị trí sẽ cắt, nếu số - âm tức tính từ phải sang của array
* độ dài sẽ lấy
* TRUE: giữ nguyên key, not sure :-))
*/
$array_slice = array_slice($toarray, -20 , 20, TRUE);
// print_r($array_slice);
fclose($p);
$tostring = implode(",", $array_slice);
/*mở file để ghi đè*/
$openfiletowrite = fopen($file, 'w');
fwrite($openfiletowrite, $tostring);
// echo $tostring;
fclose($openfiletowrite);
/* mở lại file để đọc*/
$p = fopen($file, 'r');
// feof(); => FILE END OF FILE: ==> tìm ra vị trí cuối cùng của file
echo "<b>Lastest queries</b>: ";
$file = "a.txt";
$read = fread($p, filesize($file));
$toarray2 = explode(',', $read);
foreach ($toarray2 as $value) {
// echo "<a href src = 'https://tudien.net/".$value.".html'>".$value." "."</a>";
$link = $value.",";
echo "<span id = 'lastestquery'><a href='".$value.".html'>$link</a></span>";
// echo $value.",";
}
// echo fgets($p);
// while(!feof($p)){
// //đọc file từng dòng <=> nếu chưa phải là vị trí cuối cùng của file
// $keyword = fgets($p);
// // echo ".............................................<br>";
// echo $keyword;
// /*echo "<a href src = 'https://tudien.net/test.html'>".$keyword."</a>";*/
// }
fclose($p);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment