search
Homephp教程php手册php验证URL是否合法的函数

验证URL有两种一种是利用正则表达式来验证URL是不是合适url规则了,另一个是利用函数来访问指定url看看是否可正常访问了,如果能正常访问自然就是合法的url地址了。

例子1

<?php
function isValidUrl($url) {
    $patern = &#39;/^http[s]?:\/\/&#39;. 
        &#39;(([0-9]{1,3}\.){3}[0-9]{1,3}&#39;.             // IP形式的URL- 199.194.52.184 
        &#39;|&#39;.                                        // 允许IP和DOMAIN(域名) 
        &#39;([0-9a-z_!~*\&#39;()-]+\.)*&#39;.                  // 三级域验证- www. 
        &#39;([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.&#39;.     // 二级域验证 
        &#39;[a-z]{2,6})&#39;.                              // 顶级域验证.com or .museum 
        &#39;(:[0-9]{1,4})?&#39;.                           // 端口- :80 
        &#39;((\/\?)|&#39;.                                 // 如果含有文件对文件部分进行校验 
        &#39;(\/[0-9a-zA-Z_!~\*\&#39;\(\)\.;\?:@&=\+\$,%#-\/]*)?)$/&#39;;
    if(!preg_match($patern, $url)) {
        die( &#39;您输入的URL格式有问题,请检查!&#39;);
    }
}

例子2

上面的例子只是验证url是不是正常的不代表是否可以访问了,我们可以使用如curl函数进行方法

$url = "http://www.phprm.com ";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, true);
$result = curl_exec($curl);
if ($result !== false) 
{
  $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
  if ($statusCode == 404) 
  {
    echo "URL Not Exists"
  }
  else
  {
     echo "URL Exists";
  } 
}
else
{
  echo "URL not Exists";
}

除了这个函数还可以使用php的很多函数如 file、file_get_contents()、fopen函数来进行检测了。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何解决PHP Warning: fopen(): failed to open stream: No such file or directory如何解决PHP Warning: fopen(): failed to open stream: No such file or directoryAug 19, 2023 am 10:44 AM

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory在使用PHP开发过程中,我们经常会遇到一些文件操作的问题,其中之一就是"PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory

如何解决PHP Warning: fopen(): SSL operation failed in file.php on line X如何解决PHP Warning: fopen(): SSL operation failed in file.php on line XAug 25, 2023 am 09:22 AM

如何解决PHPWarning:fopen():SSLoperationfailedinfile.phponlineX在PHP编程中,我们经常使用fopen函数来打开文件或者URL,并进行相关操作。然而,在使用fopen函数时,有时候会遇到类似于Warning:fopen():SSLoperationfailedinfile.p

如何解决PHP Warning: fopen(): failed to open stream: Permission denied如何解决PHP Warning: fopen(): failed to open stream: Permission deniedAug 20, 2023 pm 01:45 PM

如何解决PHPWarning:fopen():failedtoopenstream:Permissiondenied在开发PHP程序的过程中,我们常常会遇到一些报错信息,比如PHPWarning:fopen():failedtoopenstream:Permissiondenied。这个错误通常是由于文件或目录权限不正

Matlab中fopen函数用法Matlab中fopen函数用法Nov 28, 2023 am 11:03 AM

在Matlab中,fopen函数用于打开文件并返回文件标识符,以便后续对文件进行读取或写入操作。根据需要选择适当的权限选项来打开文件,并在操作完成后及时关闭文件。需要注意的是,打开文件后需要确保在不再需要文件时及时关闭文件,以释放系统资源。另外,如果文件打开失败或操作出错,可以通过错误处理机制进行相应的处理。

php如何使用fopen、fwrite和fclose进行文件操作?php如何使用fopen、fwrite和fclose进行文件操作?Jun 01, 2023 am 08:46 AM

在PHP开发中,对文件的操作是非常常见的。一般情况下,我们需要进行文件的读取、写入、删除等操作。其中,文件的读取可以使用fopen函数和fread函数,文件的写入可以使用fopen函数、fwrite函数和fclose函数。本文将介绍php如何使用fopen、fwrite和fclose进行文件操作。一、fopen函数fopen函数用于打开文件,它的语法如下:r

在C语言中,使用fopen()函数以写模式打开现有文件在C语言中,使用fopen()函数以写模式打开现有文件Aug 27, 2023 pm 10:33 PM

C中的fopen()方法用于打开指定的文件。我们举个例子来理解一下问题语法FILE*fopen(filename,mode)以下是使用fopen()打开文件的有效模式:&lsquo;r&rsquo;、&lsquo;w&rsquo;、&lsquo;a&rsquo;、&lsquo;r+&rsquo;、&lsquo;w+&rsquo;、&lsquo;a+&rsquo;。详细信息请访问C库函数-fopen()

如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line X如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line XAug 26, 2023 pm 12:46 PM

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectoryinfile.phponlineX在开发和运行PHP程序时,我们有时会遇到PHPWarning:fopen():failedtoopenstream:Nosuchfileor

PHP的preg_match()函数:如何使用正则表达式匹配字符串PHP的preg_match()函数:如何使用正则表达式匹配字符串Nov 04, 2023 am 11:31 AM

PHP的preg_match()函数:如何使用正则表达式匹配字符串,需要具体代码示例正则表达式在字符串处理中是非常强大和灵活的工具。在PHP中,使用preg_match()函数可以方便地进行字符串的正则匹配,从而实现各种复杂的模式匹配和替换操作。本文将介绍preg_match()函数的用法,并提供具体的代码示例来帮助读者更好地理解和应用。preg_match

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.