Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ajvanho's avatar
Level 14

How to chech is user have internet connection?

I make contact form, and want to user send post and email, only if have internet connection.

How to check internet connection?

0 likes
4 replies
_NinjaDev's avatar
Level 10

Normally i test this on the client side

yourapp.js

if(window.navigator.onLine){
	//has internet connection
}


3 likes
Wakanda's avatar

@ajvanho in php

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

}
?>
1 like

Please or to participate in this conversation.