How to ping website using php?

by mallory_cormier , in category: Third Party Scripts , a month ago

How to ping website using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , a month ago

@mallory_cormier 

To ping a website using PHP, you can use the exec function to execute a command line ping. Here's an example code snippet to ping a website:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$website = 'www.example.com';

// Command to ping the website
$command = 'ping -c 4 ' . $website;

// Execute the command and capture the output
$output = shell_exec($command);

// Display the output
echo "<pre>$output</pre>";


In this code snippet, we are using the ping command to ping the website www.example.com with 4 packets. The shell_exec function is used to execute the command, and the output is then displayed on the screen.


Please note that using shell_exec to execute external commands can have security risks, so make sure to validate and sanitize user input before using it in the command to prevent any potential security vulnerabilities.