PHP IP要求 - 5秒ごとに繰り返し、出力に日付を表​​示します。

PHP IP要求 - 5秒ごとに繰り返し、出力に日付を表​​示します。

IPアドレスを表示するPHPコードがあります。しかし、入ると一度だけ動作します。だからこのコードを使って5秒ごとに実行したいと思います。

your ip : 5.4.3.2.1 ; 18:05
your ip : 5.4.3.2.1 ; 18:10

このように。誰でもこの問題を解決するのに役立ちますか?

<?php

    // Pull contents from ip6.me
$file = file_get_contents('http://ip6.me/');

// Trim IP based on HTML formatting
$pos = strpos( $file, '+3' ) + 3;
$ip = substr( $file, $pos, strlen( $file ) );

// Trim IP based on HTML formatting
$pos = strpos( $ip, '</' );
$ip = substr( $ip, 0, $pos );

// Output the IP address of your box
echo "My IP address ; $ip";

?>

ベストアンサー1

5秒ごとに既存のスクリプトを実行し、現在の日付とともに出力を時間と分で表示するには、次のようにします。

#!/bin/sh

while :
do
  printf '%s ; %s\n' "$(./myip.py)" $(date +%H:%M)
  sleep 5
done

Control-Cを押して無限ループを終了します。無限ループと「各反復の間に5秒待機」に加えて、このprintfステートメントはスクリプトの出力(ここで命名myip.py)を取得し、セミコロンと出力を追加しますdate +%H:%M

おすすめ記事