Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:53:43+00:00 2026-06-01T00:53:43+00:00

I want a PHP script which allows you to ping an IP address and

  • 0

I want a PHP script which allows you to ping an IP address and a port number (ip:port). I found a similar script but it works only for websites, not ip:port.

<?php

function ping($host, $port, $timeout) 
{ 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("www.google.com", 80, 10);

?>

I want this for a game server.

The idea is that I can type in the IP address and port number, and I get the ping response.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:53:44+00:00Added an answer on June 1, 2026 at 12:53 am

    I think the answer to this question pretty much sums up the problem with your question.

    If what you want to do is find out whether a given host will accept
    TCP connections on port 80, you can do this:

    $host = '193.33.186.70'; 
    $port = 80; 
    $waitTimeoutInSeconds = 1; 
    if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){   
       // It worked 
    } else {
       // It didn't work 
    } 
    fclose($fp);
    

    For anything other than TCP it will be more difficult (although since
    you specify 80, I guess you are looking for an active HTTP server, so
    TCP is what you want). TCP is sequenced and acknowledged, so you will
    implicitly receive a returned packet when a connection is successfully
    made. Most other transport protocols (commonly UDP, but others as
    well) do not behave in this manner, and datagrams will not be
    acknowledged unless the overlayed Application Layer protocol
    implements it.

    The fact that you are asking this question in this manner tells me you
    have a fundamental gap in your knowledge on Transport Layer protocols.
    You should read up on ICMP and TCP, as well as the OSI Model.

    Also, here’s a slightly cleaner version to ping to hosts.

    // Function to check response time
    function pingDomain($domain){
        $starttime = microtime(true);
        $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
        $stoptime  = microtime(true);
        $status    = 0;
    
        if (!$file) $status = -1;  // Site is down
        else {
            fclose($file);
            $status = ($stoptime - $starttime) * 1000;
            $status = floor($status);
        }
        return $status;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a PHP script which allows me to modify and update a
I want a PHP script that adds some data at the end of a
I want to have a PHP script send a XML formatted string to another
In a PHP script I want to get just the URL of the top
I want to execute a php-script from php that will use different constants and
I want to write a php script that keeps the apache_log file open and
I want to run a php script from the command line that is always
I want to write a php script to compare rows from the database and
I want to create an HTTPURLConnection to a PHP script and get the HTTP
i want to pass large variables to a PHP script on another server with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.