PHP Link Kontrol Etme

Aşağıdaki fonksiyon ile php ile bir linkin gerçek bir link olup olmadığını kontrol edebilirsiniz, fonksiyon, link geçerli bir link ise true, değilse false değeri döndürecektir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
 
function is_valid_url ( $url )
{
		$url = @parse_url($url);
 
		if ( ! $url) {
			return false;
		}
 
		$url = array_map('trim', $url);
		$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
		$path = (isset($url['path'])) ? $url['path'] : '';
 
		if ($path == '')
		{
			$path = '/';
		}
 
		$path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';
 
		if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) )
		{
			if ( PHP_VERSION >= 5 )
			{
				$headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
			}
			else
			{
				$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
 
				if ( ! $fp )
				{
					return false;
				}
				fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
				$headers = fread ( $fp, 128 );
				fclose ( $fp );
			}
			$headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
			return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
		}
		return false;
}
 
$linkler=array ("http://www.site.com","http://www.site.com/","www.site.com","site.com","https://www.site.com","https://www.site.com/","ftp://site.com","ftp://site.com/"); //kontrol için linkleri arka arkaya dizdik
 
	foreach ($linkler as $kontrol) { // teker teker tüm linkleri kontrol edelim şimdi
	echo is_valid_url($kontrol)?strip_tags($kontrol)." geçerli bir linktir":strip_tags($kontrol)." geçerli bir link değildir";
	echo "<br />";
	}
 
?>

kaynak: roscripts

VN:F [1.9.11_1134]
Rating: 6.2/10 (6 votes cast)
VN:F [1.9.11_1134]
Rating: -1 (from 1 vote)
PHP Link Kontrol Etme, 6.2 out of 10 based on 6 ratings