如果连接到互联网,在 php 脚本中确定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4860365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine in php script if connected to internet?
提问by Steve
How can I check if I'm connected to the internet from my PHP script which is running on my dev machine?
如何通过我的开发机器上运行的 PHP 脚本检查我是否已连接到 Internet?
I run the script to download a set of files (which may or may not exist) using wget. If I try the download without being connected, wget proceeds to the next one thinking the file is not present.
我运行脚本以使用 wget 下载一组文件(可能存在也可能不存在)。如果我在未连接的情况下尝试下载,wget 会继续下一个,认为文件不存在。
回答by blasteralfred Ψ
<?php
function is_connected()
{
$connected = @fsockopen("www.example.com", 80);
//website, port (try 80 or 443)
if ($connected){
$is_conn = true; //action when connected
fclose($connected);
}else{
$is_conn = false; //action in connection failure
}
return $is_conn;
}
?>
回答by Gabi Purcaru
You can always ping good 'ol trusty google:
你总是可以 ping good 'ol trusty google:
$response = null;
system("ping -c 1 google.com", $response);
if($response == 0)
{
// this means you are connected
}
回答by user28864
This code was failing in laravel 4.2 php framework with an internal server 500 error:
此代码在 laravel 4.2 php 框架中失败,并出现内部服务器 500 错误:
<?php
function is_connected()
{
$connected = @fsockopen("www.some_domain.com", 80);
//website, port (try 80 or 443)
if ($connected){
$is_conn = true; //action when connected
fclose($connected);
}else{
$is_conn = false; //action in connection failure
}
return $is_conn;
}
?>
Which I didn't want to stress myself to figure that out, hence I tried this code and it worked for me:
我不想强迫自己弄清楚这一点,因此我尝试了这段代码,它对我有用:
function is_connected()
{
$connected = fopen("http://www.google.com:80/","r");
if($connected)
{
return true;
} else {
return false;
}
}
Please note that: This is based upon the assumption that the connection to google.com is less prone to failure.
请注意:这是基于与 google.com 的连接不太容易出现故障的假设。
回答by hilbiazhar
The accepted answer did not work for me. When the internet was disconnected it threw a php error. So I used it with a little modification which is below:
接受的答案对我不起作用。当互联网断开连接时,它抛出了一个 php 错误。所以我用它做了一些修改,如下所示:
if(!$sock = @fsockopen('www.google.com', 80))
{
echo 'Not Connected';
}
else
{
echo 'Connected';
}
回答by wimvds
Why don't you fetch the return code from wget to determine whether or not the download was successful? The list of possible values can be found at wget exit status.
为什么不从 wget 中获取返回码来确定下载是否成功?可以在wget exit status 中找到可能值的列表。
On the other hand, you could use php's curl functionsas well, then you can do all error tracking from within PHP.
另一方面,您也可以使用 php 的curl 函数,然后您可以在 PHP 中进行所有错误跟踪。
回答by Maciej ?ebkowski
There are various factors that determine internet connection. The interface state, for example. But, regardles of those, due to the nature of the net, proper configuration does not meen you have a working connection.
有多种因素决定互联网连接。例如接口状态。但是,不管怎样,由于网络的性质,正确的配置并不意味着你有一个工作连接。
So the best way is to try to download a file that you're certain that exists. If you succeed, you may follow to next steps. If not, retry once and then fail.
因此,最好的方法是尝试下载您确定存在的文件。如果成功,您可以按照下一步操作。如果没有,请重试一次,然后失败。
Try to pick one at the destination host. If it's not possible, choose some major website like google or yahoo.
尝试在目标主机上选择一个。如果不可能,请选择一些主要网站,例如 google 或 yahoo。
Finally, just try checking the error code returned by wget. I bet those are different for 404-s and timeouts. You can use third parameter in exec
call:
最后,只需尝试检查 wget 返回的错误代码。我敢打赌,对于 404-s 和超时,这些是不同的。您可以在exec
调用中使用第三个参数:
string exec ( string $command [, array &$output [, int &$return_var ]] )
string exec ( string $command [, array &$output [, int &$return_var ]] )
回答by cagcak
/*
* Usage: is_connected('www.google.com')
*/
function is_connected($addr)
{
if (!$socket = @fsockopen($addr, 80, $num, $error, 5)) {
echo "OFF";
} else {
echo "ON";
}
}
回答by Roy T.
You could ping to a popular site or to the site you're wgetting from (like www.google.nl) then parse the result to see if you can connect to it.
您可以 ping 到一个受欢迎的站点或您从中获取的站点(例如 www.google.nl),然后解析结果以查看是否可以连接到它。
<?php
$ip = '127.0.0.1'; //some ip
exec("ping -n 4 $ip 2>&1", $output, $retval);
if ($retval != 0) {
echo "no!";
}
else
{
echo "yes!"; }
?>
回答by Dietrich Epp
Just check the result of wget
. A status code of 4 indicates a network problem, a status code of 8 indicates a server error (such as a 404). This only works if you call wget for each file in sequence, rather than once for all the files.
只需检查wget
. 状态代码 4 表示网络问题,状态代码 8 表示服务器错误(例如 404)。这仅在您按顺序为每个文件调用 wget 时才有效,而不是为所有文件调用一次。
You can also use libcurl
with PHP, instead of calling wget. Something like:
您也可以libcurl
与 PHP 一起使用,而不是调用 wget。就像是:
foreach (...) {
$c = curl_init($url);
$f = fopen($filepath, "w")
curl_setopt($c, CURLOPT_FILE, $f);
curl_setopt($c, CURLOPT_HEADER, 0);
if (curl_exec($c)) {
if (curl_getinfo($c, CURLINFO_HTTP_CODE) == 200) {
// success
} else {
// 404 or something, delete file
unlink($filepath);
}
} else {
// network error or server down
break; // abort
}
curl_close($c);
}
回答by Lost Koder
This function handles what you need
这个函数处理你需要的
function isConnected()
{
// use 80 for http or 443 for https protocol
$connected = @fsockopen("www.example.com", 80);
if ($connected){
fclose($connected);
return true;
}
return false;
}