注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天高云淡的博客

A bad beginning makes a bad ending.

 
 
 

日志

 
 

FSockOpen Tutorial  

2010-05-24 09:19:01|  分类: PHP |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Fsockopen is used to open a socket connection via PHP. In this tutorial we will show you how to test if a port is in use. This can be used as a status script for services.

FSockOpen

fsockopen  ( string $hostname  [, int $port= -1  [, int &$errno  [, string &$errstr  [, float $timeout= ini_get("default_socket_timeout")  ]]]] )

Common Service Ports
HTTP: 80
MYSQL: 3306
FTP: 21
SSH: 22
DNS: 53
POP: 110
SMTP: 25
Please note that these are the common service ports. Although I have listed some common ports you can use this script with any port. If you or your host has configured the services these ports may be different also MYSQL will show as being offline unless the script is running on the same server as the mysql server or mysql has been configured to allow external access.

Tutorial
So what we will do in this tutorial is test if HTTP is running.

<?php
/**
* Site: example.com
* Port: 80 - HTTP
* Timeout: 5 Seconds
*/

$httpcheck = @fsockopen("example.com", "80", $errno, $errstr, 5); // @ will mute any errors. This is needed so that should the port be down then it will not show any errors.
if($httpcheck){ // Check if $httpcheck is true.
echo "http on example.com is running."; // Echo Message if port is up.
}else{ // If $httpcheck finds the port down
echo "http isnt running on example.com."; // Echo Message if port is down
}
 
?>

The code above is how you would check if http is running on example.com. There are 2 unused items in the tutorial $errono and $errstr. These are used to discover what the error was. Below is an example on how you could use these.

<?php
/**
* Site: example.com
* Port: 80 - HTTP
* Timeout: 5 Seconds
*/

$httpcheck = @fsockopen("example.com", "80", $errno, $errstr, 5); // @ will mute any errors. This is needed so that should the port be down then it will not show any errors.
if($httpcheck){ // Check if $httpcheck is true.
echo "http on example.com is running."; // Echo Message if port is up.
}else{ // If $httpcheck finds the port down
echo "Error: ".$errstr."(".$errstr.")"; // Echo Message if port is down
}
 
?>

Thank you for reading. If you need any help or like this tutorial please leave a comment.

  评论这张
 
阅读(733)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017