确定在PHP脚本是否连接到互联网? [英] Determine in php script if connected to internet?

查看:90
本文介绍了确定在PHP脚本是否连接到互联网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查开发机器上运行的PHP脚本是否已连接到互联网?

How can I check if I'm connected to the internet from my PHP script which is running on my dev machine?

我运行脚本以使用wget下载一组文件(可能存在或可能不存在).如果我在没有连接的情况下尝试下载,则wget会继续进行下一个下载,认为文件不存在.

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.

推荐答案

<?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;

}
?>

这篇关于确定在PHP脚本是否连接到互联网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆