This tutorial will guide you through how to create an application which allows for a user to input a website and reveal whether it's their connection or whether the website is actually offline.
For more information on the used elements, click on their name.
Let's begin by declaring our PHP statements.
<?php
We now have the beginning of our PHP file. That's the first step done; now we need to move on to the error checking. This is done so that we can find out whether we should process the input data, or ask for it.
if(isset($_GET['url'])) {
$url = $_GET['url'];
}
The above code runs an error check. Let's now go through this line by line.
if(isset($_GET['url'])) {
This is the isset function. The responsibility of this is to check whether the URL from the form is sent to the script.
The next line is setting the variable $url to the contents of the requested form input box.
The final line closes the IF statement.
Now, we set the ELSE function.
else {
echo "<h1>Please enter a URL</h1>";
}
Again, going through this line by line allows us to know what the code is doing.
The first line, else, tells the server to go to this only if the above URL is not set.
The next line, echo, outputs the text "Please enter a URL".
We then close our statement.
Now, we're almost set to go into the configuration of the script - this will make it do something!
We will be using more error checking now. This now checks if the variable $url was correctly set.
Let's now move onto the advanced part, the part which is executed when the variable $url is set.
$fp = @fsockopen("$url", 80
, $errno, $errstr, 30
);
Wow. That's a lot, isn't it? So let's find out what it does.
We have told PHP to set the variable $fp the contents of the fsockopen command. The function 'fsockopen' is the simple way of connecting - or pinging - a URL using PHP. We have used our $url as the website to ping, the port 80 is the default web server port. Enter http://www.hulldo.co.uk:80 into your browser - it's the same thing.
The next functions are not really asking for you to understand. The key thing is the 30 which means 30 second timeout, if no reply is received.
We are now ready to move on to set the output.
if (!$fp) {
$data = "$url - <span style='color:red;'>Offline</span>";
}
What we're doing here is telling PHP to set $data to hold the contents with the quotes; this displays the website is offline. Why? Well, the !$fp represents PHP not being able to establish a connection.
So, what if PHP can establish a connection? Let's set the code for that.
else {
$data = "$url - <span style='color:green;'>Online</span>";
fclose($fp);
}
Everything is similar. Apart from the fclose. What is this? It's to tell PHP to close the connection to the tested website. That's all good. Next, we move onto the output of the variables.
echo $data;
}
else {
Looking above, we have echoed the output. We also have an else statement. This is done to tell PHP to execute another set of commands if the variable $url wasn't set when we loaded the page. We need a form. This form needs to allow data input for the script to work. Let's move on to creating this form.
?>
We've closed the PHP statement - for now. Let's move to the HTML form.
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="GET">
<input type="text" name="url" value="Enter the web address without http://" size="50"/><br />
<input type="submit" value="Submit" />
</form>
As this tutorial is more PHP based, we're not going into too much detail on the form. Basically, the action is to use itself for the command and have an input named url to allow the script to collect the data. We're now nearly complete.
We have open PHP tags waiting to be closed. You knew that, right? The else statement, which is still open? Let's close it.
<?
}
?>
That concludes this tutorial. If you're wondering how to use it, simply enter a URL without the http:// value.
All of the code for this tutorial can be seen below.
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
}
else {
echo "<h1>Please enter a URL</h1>";
}
if(isset($url)) {
$fp = @fsockopen("$url", 80
, $errno, $errstr, 30
);
if (!$fp) {
$data = "$url - <span style='color:red;'>Offline</span>";
}
else {
$data = "$url - <span style='color:green;'>Online</span>";
fclose($fp);
}
echo $data;
}
else {
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="GET">
<input type="text" name="url" value="Enter the web address without http://" size="50"/><br />
<input type="submit" value="Submit" />
</form>
<?
}
?>
This tutorial is up to date as of 01 August 09. While you're here, why not check out more tutorials? You could also subscribe to our RSS Feed, or even check out our Twitter - follow us for the latest updates.
For video tutorials, subscribe to our YouTube channel. Feel free to check out our blog for more updates.