php variable $_SERVER['SERVER_ADDR'] is blank when using crontab -
i have small script gets basic information server ip-, , mac-address of server script running on, posting results mysql database, works fine , code shown below. when use cronjob execute script ip address being stored, reason $_server['server_addr']
blank when being executed cronjob.
this script runs every min, , run on multi raspberry pi's can tell connected share job requests.
$mac_address = getmaclinux(); $server_ip = $config['server_ip']; $client_ip = $_server['server_addr']; $client_name = $config['client_name']; $ch = curl_init(); $url = "{$server_ip}/checkin0.php"; echo "url = {$url}"."<br>"; curl_setopt($ch, curlopt_url,$url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, "client_ip={$client_ip}&client_name={$client_name}&client_mac={$mac_address}"); curl_setopt($ch, curlopt_returntransfer, true); $output = curl_exec ($ch); // execute curl_close ($ch); // close curl handle var_dump($output); // show output
i found following
function getserveraddress() { if(isset($_server["server_addr"])) return $_server["server_addr"]; else { // running cli if(stristr(php_os, 'win')) { // rather hacky way handle windows servers exec('ipconfig /all', $catch); foreach($catch $line) { if(eregi('ip address', $line)) { // have seen exec return "multi-line" content, hack. if(count($linecount = split(':', $line)) == 1) { list($t, $ip) = split(':', $line); $ip = trim($ip); } else { $parts = explode('ip address', $line); $parts = explode('subnet mask', $parts[1]); $parts = explode(': ', $parts[0]); $ip = trim($parts[1]); } if(ip2long($ip > 0)) { echo 'ip '.$ip."\n"; return $ip; } else ; // todo: handle failure condition. } } } else { $ifconfig = shell_exec('/sbin/ifconfig eth0'); preg_match('/addr:([\d\.]+)/', $ifconfig, $match); return $match[1]; }
Comments
Post a Comment