名站导航为爱好php程序的朋友们提供php相关的教程知识。
PhP程序 验证URL地址分为二个部分:第一个部分是PhP程序 验证URL地址是否是有效的URL地址;第二个部分是PhP程序 检验该URL地址是否存在。也就是对该URL地址的真实性的检验。解析URL地址我们用以前讲的parse_url()这个函数来实现。先不多说,看具体代码如下:
<?php // A function to do a sanity check on a URL ... Since we are assuming // that this is coming from user input, we will require it be a full // URL, and not a relative one function validate_url($url) { // Use parse_url to break the URL into parts for us: $up = parse_url($url); // If scheme, host, or path don't exist, or complete failure: INVALID if (!$up || !$up['scheme'] || !$up['host'] || !$up['path']) { return false; } // If the scheme is anything besides http(S) or ftp: Fail if (!( ($up['scheme'] == 'http') || ($up['scheme'] == 'https') || ($up['scheme'] == 'ftp')) ) { return false; } // We made it here, it looks good to us. return true; } // Function that will actually take a url, and attempt to contact the server // if it can't access the remote file, it returns false, else true. function check_url($url) { // Request only the headers, no reason to download the whole file // Note that this call will only handle https connections if PhP程序 was // compiled with SSL support. $output = *get_headers($url); // Return appropriately return $output ? true : false; } // Test a few URLs $urls = array('http://eliw.com/', 'http://php.net/get_headers', 'gopher://bob.com/', 'https://lawn.tractor/models/1.php', 'http://hubblesite.org/news/2006/01/', 'http://digg.com/'); // Loop over these, check if they appear valid, if they do, try to access foreach ($urls as $r) { // If this does not validate: if (!(validate_url($r))) { // Set the display accordingly: $disp = 'Did not validate!'; } else { // Try to access it, and set display accordingly $disp = check_url($r) ? 'GOOD!' : 'Could not contact...'; } // Display this echo "<p>{$r} = {$disp}</p>\n"; } ?>
好了关于php程序的知识就说到这里希望可以帮助需要的朋友。,使用PhP程序实现URL的转码和反转码