Jump to content

php host status from php mysql database


Go to solution Solved by Barand,

Recommended Posts

Help me!!

Seem when i run the scripts, the output seems wrong :(

 

This is the output...

CG5XdYa.png?1

The 1st and 3rd entry is true, online

 

But the rest should be OFFLINE but the output shows all ONLINE

 

I think the problem is from foreach loop but I cannot figure out how to solve the problem

 

Please help me... :(

<?php
include 'connector/connect.php';
?>
<meta http-equiv="refresh" content="6" >
<style>
    tbody > tr:nth-child(2n+1) > td, tbody > tr:nth-child(2n+1) > th {
        background-color: #ededed;
    }
    table{
        width: 70%;
        margin: auto;
        border-collapse: collapse;
        box-shadow: darkgrey 3px;
    }
    thead tr {
        background-color: #36c2ff;
    }
</style>

<h1 align="center">Server Availability</h1>

<table border="1">
    <thead>
	
        <tr>
	    <th>Entry</th>
            <th>Host</th>
            <th>Name</th>
            <th>Location</th>
            <th>Description</th>
            <th>Availability</th>
        </tr>
		
    </thead>
	
	<tbody>
	
    <?php
    $sql = "SELECT * FROM host_entry";
    $no  = 1;
	
    $dbhi = $dbh->query($sql);

    foreach ($dbhi->fetchAll(PDO::FETCH_BOTH) as $data) {
    ?>
        <tr>
            <td><?php echo $no++; ?></td>
            <td><?php echo $data['host_server']; ?></td>
            <td><?php echo $data['host_name']; ?></td>
            <td><?php echo $data['host_location']; ?></td>
            <td><?php echo $data['host_desc']; ?></td>
			<td>
		<?php  
			$ip = $data["host_server"];
			
                        // Run the ping to the IP
			exec ("ping -n 1 -w 1 $ip", $ping_output);

			if(preg_match("/Reply/i", $ping_output[2])) {
			    echo "<font color='green'><strong>Online!</strong></font>";
			}else{
			    echo "<font color='red'><strong>Offline!</strong></font>";
			}
						
                ?>
			</td>
        </tr>
    <?php
    }
    ?>
	
    </tbody>
</table>
Link to comment
https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/
Share on other sites

  • Solution

Clear the output array before each ping

// Run the ping to the IP
            $ping_output = array();
            exec ("ping -n 1 -w 1 $ip", $ping_output);
Edited by Barand

Some simple debugging on your part would have saved you two days of misery.

 

Had you placed something like this to check the results

echo '<pre>',print_r($ping_output, true),'</pre>';

after each ping you would have seen for yourself what was happening.

Barand, the scripts work great..:)

 

I want to ask you, does my exac command in foreach..loop work at right way or not??

What i mean was, I want to create a ping sites that display status of ip/server...

It include other computer, servers, website, and so on...

 

So i use this this exac command to ping to other ip/server...

if ok...could you suggest we or give examples of well writing exac command in foreach??

 

if not, its ok...i`m just asking..:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.