PHP获取动态IP并记录到文件(第三版)
改了几个错误,改了一下记录文件格式,缩减体积,加了个什么点击才显示所有数据详情的功能。
之前的帖子:
PHP获取动态IP并记录到文件(改进版) – arya142!
传入url形式: https://www.***.com/test.php?ddnsurl=****.gicp.net&ddnsip=111.111.111.111&key=password
新的代码如下:
|
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
<?php $datafile="ddnsip.data"; if(!is_writeable($datafile)) echo("<font color=red>File is not writeable!</font><br>\n");############### //点击Contents of records链接后,在contents段落中显示内容。 if (isset($_GET['load'])) { $lines = @file($datafile); $content = ''; for($n = count($lines)-1; $n >= 0; $n--) { $line = trim($fp[$n] ?? $lines[$n]); // 不转义 ,保留原样 $content .= $line . "<br>"; } echo $content; exit; } //这个密码自己设置,也就是做个样子,其实并没有什么卵用。 if(is_array($_GET)&&count($_GET)>0&&$_GET["key"]=="password") { if(isset($_GET["ddnsip"])&&isset($_GET["ddnsurl"])) { $ddnsip = $_GET['ddnsip']; $ddnsurl = $_GET['ddnsurl']; $ddnstext = $ddnsurl . " - " . $ddnsip . " - [".date('Y-m-d H:i:s')."]\n";//记录进data文件的格式。 $maxline = 500; $fp = file($datafile); $file = fopen($datafile,"a"); $try = 0; LOCKFILE: if(flock($file , LOCK_EX)) { for ($x=(count($fp)-1);$x>=0;$x--) { if(substr($fp[$x],0,strpos($fp[$x]," -")) == $ddnsurl) { if($ddnsip != substr($fp[$x],strpos($fp[$x],"- ")+2,strrpos($fp[$x]," -")-strpos($fp[$x],"- ")-2)) { fwrite($file,$ddnstext); echo("ddnsip updated.<br>");############### } break; } } if($x==-1) { fwrite($file,$ddnstext); echo("ddnsip added.<br>");############### } flock($file , LOCK_UN); fclose($file); } else { sleep(5); $try = $try + 1; if($try <= 5) { goto LOCKFILE; } } ############shift if lines max########### $fp = file($datafile); if(count($fp)>$maxline) { while(count($fp)>$maxline){array_shift($fp);} $file = fopen($datafile,"w"); if(flock($file , LOCK_EX)) { for($n=0;$n<count($fp);$n++) { fwrite($file,$fp[$n]); } flock($file , LOCK_UN); } fclose($file); echo("Records shifted.<br>\n");############### } } else { echo("Parameters is not set!<br>\n"); $fp = file($datafile); echo("<b>Records:</b>".count($fp)."<br>\n"); echo("<b>Summary:</b><br>\n——————————————————————————————<br>\n"); for($n=count($fp)-1;$n>=1;$n--) { $first_name=substr($fp[$n],0,strpos($fp[$n]," -")); for($m=$n-1;$m>=0;$m--) { if($first_name == substr($fp[$m],0,strpos($fp[$m]," -"))) { array_splice($fp,$m,1); $n--; } } } rsort($fp); for($n=count($fp)-1;$n>=0;$n--) { $ipaddr = substr($fp[$n],strpos($fp[$n],"- ")+2,strrpos($fp[$n]," -")-strpos($fp[$n],"- ")-2); if(strpos($ipaddr,':')!==false) { echo(substr($fp[$n],0,strpos($fp[$n]," -")+3) . "<a href=\"http://[" . $ipaddr . "]:1987\" about=_blank>" . $ipaddr . "</a>" . substr($fp[$n],strrpos($fp[$n]," -"),strlen($fp[$n])-strrpos($fp[$n]," -"))) . "<br>"; } else { echo(substr($fp[$n],0,strpos($fp[$n]," -")+3) . "<a href=\"http://" . $ipaddr . ":1987\" about=_blank>" . $ipaddr . "</a>" . substr($fp[$n],strrpos($fp[$n]," -"),strlen($fp[$n])-strrpos($fp[$n]," -"))) . "<br>"; } } echo("<br>\n"); echo "<b><a href=\"javascript:;\" onclick=\"showMore()\">Contents of records:</a></b><br>\n——————————————————————————————<br>\n"; echo "<p id=\"contents\" style=\"white-space:pre-wrap;\"></p>"; } } else { echo("No parameters!<br>\n"); } ?> <script> function showMore() { fetch('?load=1') .then(res => res.text()) .then(data => { document.getElementById('contents').innerHTML = data; }); } </script> |