Anunturi:

Connect cs.click4trick.com!


Checking links with PHP

Here you can post and read tutorials.

Moderator: Super Moderatori

Checking links with PHP

Mesajde chessh pe Mie Mai 14, 2008 12:22 am

In this tutorial I will teach you to make a script that automatically checks if a link still exists. The programming language we will use is PHP.

First of all create a .php file (link-checker.php).

Add the PHP start tag <?php.

Now create a variable, $website and assign the link to it. The link must be http://www.domain.tld.

Create another variable, $works and assign it with @fsockopen($website, $port, $errno, $errstr, $timeout).
We are using the fsockopen function (Initiates a socket connection to the resource specified) with:
-$website (the hostname)
-$port (the port number, 80)
-$errno (If provided, holds the system level error number that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.)
-$errstr (the error message as a string)
-$timeout (the connection timeout in seconds, 30).

After doing that, we will use the if statement. If $works is true, it means that the link is still working and it will return the message http://www.itstart.org works fine!, with an anchor to itstart. If $works is not true, it means that the link is no longer working and it will return the message http://www.itstart.org is down. We'll be right back!, also with an anchor to itstart.

That's it! Now you've created a checking links script using PHP! Congrats!
Here is the full code:

Cod: Selectaţi tot
<?php
$website = 'www.itstart.org';
$port = 80;
$timeout = 30;
$works = @fsockopen($website, $port, $errno, $errstr, $timeout);

if($merge) {
echo ''.$site.' works fine!';
} else {
echo ''.$site.' is down. We'll be right back!';
}
?>


Note: You can change http://www.itstart.org with the link you want to check and run the file on a host. Enjoy!
Intelligence plus character - that is the goal of true education.
Avatar utilizator
chessh
C4T Father
C4T Father
 
Mesaje: 4329
Membru din: Mie Ian 31, 2007 11:41 pm
Locaţie: In Nicole Scherzinger.

Înapoi la Tutorials

Cine este conectat

Utilizatori ce navighează pe acest forum: Niciun utilizator înregistrat şi 1 vizitator