how to check internet connection from php code

how to check internet connection from PHP code?

In my current project, I need to check that our system is connected to the internet or not.

Please let me know how to check this?

Asked on July 29, 2021 in info.
Add Comment
2 Answer(s)
<?php
//checking connection with @fopen
if ( @fopen(“https://blog.eduguru.in”, “r”) )
{
print “You are connected to the internet.”;
}
else
{
print “You seem to be offline. Please check your internet connection.”;
}
?>
Answered on July 29, 2021.
Add Comment

You can also do this:

 

<?php
switch (connection_status())
{
case CONNECTION_NORMAL:
$txt = ‘Connection is in a normal state’;
break;
case CONNECTION_ABORTED:
$txt = ‘Connection aborted’;
break;
case CONNECTION_TIMEOUT:
$txt = ‘Connection timed out’;
break;
case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
$txt = ‘Connection aborted and timed out’;
break;
default:
$txt = ‘Unknown’;
break;
}

echo $txt;
?>

for information on connection status function in PHP, follow the below link

https://blog.eduguru.in/php/php-connection_status-function

Answered on July 29, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.