Hello DaniWeb, I was wondering how I can have a server status in the Title bar. Such as: (Website name) - Server is online! or (Site Name) Server is offline!

Dani AI

Generated

A few practical notes and a simple pattern that fits the replies already here.

As observed, a server that is down cannot deliver a page to report its own status. The reliable solution is to have a separate status check (either a small background job on the site or an external monitor) record the last-known state in a tiny JSON/file. The site can then either render that value into the <title> on the server side (as suggested) or fetch the JSON after page load and update document.title dynamically. 's socket idea is valid in principle; many prefer HTTP checks via cURL for robustness and easier handling of timeouts and redirects.

Example workflow:

  • A cron/worker polls target hosts every minute and writes /var/www/status/site.json.
  • The public page reads that cached JSON (very fast) or the client fetches it and updates the title asynchronously.

A minimal PHP check (server-side worker) and client update example:

<?php
// status.php (worker or endpoint) - use a cron worker in production
$host = 'https://example.com';
$ch = curl_init($host);
curl_setopt_array($ch, [
  CURLOPT_NOBODY => true,
  CURLOPT_CONNECTTIMEOUT => 2,
  CURLOPT_TIMEOUT => 3,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo json_encode(['status' => ($code && $code < 600) ? 'online' : 'offline']);
curl_close($ch);
// fetch and update title
fetch('/status.php')
  .then(r => r.json())
  .then(j => { document.title = document.title.split(' - ')[0] + ' - Server is ' + j.status; })
  .catch(() => { /* keep original title or show unknown */ });

Notes and cautions: use short timeouts and retries to avoid false negatives; sanitize inputs if exposing host parameters; if the monitored host is the same machine, rely on an external monitor or cached state so the title can still report "offline." For cURL options see PHP cURL manual. For updating the page title see MDN Document.title.

Recommended Answers

All 3 Replies

Have you considered that if the server is offline, how would it serve a web page telling the user such?

R.

Member Avatar for Member #334542

Why you not pass the variable value that was your status to the title heading. Its possible.

If you want to display the online/offline information on another website or server, you could try

<html> 

<head> 
<title>
<?php
function GetServerStatus($URL, $PORT)
{
$status = array("OFFLINE", "ONLINE");
$fp = @fsockopen($site, $port, $errno, $errstr, 2);
if (!$fp) {
    return $status[0];
} else
  { return $status[1];}
}
?>
</title> 
</head>

<body>
Bla Bla Bla

</body>

Calum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.