Page 1 of 1
problem with this little php ping code
Posted: Thu Jul 29, 2010 10:43 am
by Gemster
Hi, im new to php if statments. i have a little ping code that i need help with.
Code: Select all
<?php
if(system('ping -c 1 64.85.163.6')) {
echo "online";
} else {
echo "offline";
}
?>
Baiscally i want it to ping that ip and if it pings then echo Online and if not then echo Offline, that all i want it to do
Thanks for any help
Gemster
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 12:22 pm
by jraede
What's wrong with it? Are you getting any error messages?
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 1:17 pm
by Gemster
jraede wrote:What's wrong with it? Are you getting any error messages?
It outputs this
PING 64.85.163.6 (64.85.163.6) 56(84) bytes of data. 64 bytes from 64.85.163.6: icmp_seq=1 ttl=54 time=98.4 ms --- 64.85.163.6 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 98.439/98.439/98.439/0.000 ms online
I want it to output: 64.85.163.6 Online and if it dont ping, 64.85.163.6 Offline
Thanks
Gemster
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 1:21 pm
by Jade
Try this:
Code: Select all
<?php
$ip = '64.85.163.6';
if(system('ping -c 1 $ip', $result)) {
echo "$ip online";
} else {
echo "$ip offline";
}
?>
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 1:24 pm
by jraede
The system function displays the output of whatever command you pass to it. Use exec() instead.
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 2:27 pm
by Gemster
ok thanks guys, just 1 question,
this now works:
Code: Select all
<?php
if(exec('ping -c 1 64.85.163.6')) {
echo "IP 64.85.163.6 Is <b><u>Online</b></u>";
} else {
echo "IP 64.85.163.6 Is <b><u>Offline</b></u>";
}
?>
But this does not work:
Code: Select all
<?php
$ip = '64.85.163.6';
if(exec('ping -c 1 $ip')) {
echo "IP $ip Is <b><u>Online</b></u>";
} else {
echo "IP $ip Is <b><u>Offline</b></u>";
}
?>
The top code works fine an says IP 64.85.163.6 Is Online but the second code says IP 64.85.163.6 Is Offline
Any help with that would be great as i like the idea of useing $ip.
Thanks
Gemster
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 3:00 pm
by jraede
PHP only fills in variables in strings if you use double quotes ("). If you use single quotes, it will take everything literally, so it will try to ping the IP $i.
Re: problem with this little php ping code
Posted: Thu Jul 29, 2010 3:56 pm
by Gemster
Sorry, I dont understand :/
Maybe if u fix the code above and post that then I will understand what you meen.
Thanks
Gemster
Re: problem with this little php ping code
Posted: Mon Aug 02, 2010 10:30 am
by Jade
Code: Select all
<?php
$ip = '64.85.163.6';
if(exec('ping -c 1' . $ip)) {
echo "IP $ip Is <b><u>Online</b></u>";
} else {
echo "IP $ip Is <b><u>Offline</b></u>";
}
?>
Re: problem with this little php ping code
Posted: Mon Aug 02, 2010 4:03 pm
by jraede
Sorry for the delay. To clarify:
Code: Select all
$variable = 'test';
echo "The variable is $variable"; // This will display: The variable is test
echo 'The variable is $variable'; // This will display: The variable is $variable
PHP doesn't parse strings in single quotes for variables, so it will display purely the content of the string. Double quotes are parsed for variables and their corresponding values are inserted. Naturally, this takes a bit longer, so if you have a large application, ideally you should use single-quotes unless there's a variable involved to shave some microseconds off processing time. It's barely noticeable though.