11c08-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 56 行

PHP
56
字号
<?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 onefunction 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 accessforeach ($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";}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?