如何使用PHP检查URL是外部URL还是内部URL?
我正在使用此循环获取页面的所有AHREF:如何使用PHP检查URL是外部URL还是内部URL?,php,html,backend,Php,Html,Backend,我正在使用此循环获取页面的所有AHREF: foreach($html->find('a[href!="#"]') as $ahref) { $ahrefs++; } 我想这样做: foreach($html->find('a[href!="#"]') as $ahref) { if(isexternal($ahref)) { $external++; } $ahrefs++; } 其中isexternal是一个函数 functio
foreach($html->find('a[href!="#"]') as $ahref) {
$ahrefs++;
}
我想这样做:
foreach($html->find('a[href!="#"]') as $ahref) {
if(isexternal($ahref)) {
$external++;
}
$ahrefs++;
}
其中isexternal是一个函数
function isexternal($url) {
// FOO...
// Test if link is internal/external
if(/*condition is true*/) {
return true;
}
else {
return false;
}
}
救命啊 您可能需要检查链接是否在同一个域中。但是,只有当所有href属性都是绝对属性且包含域时,这才有效。像/test/file.html这样的相对文件比较棘手,因为其中的文件夹可能与域同名。。因此,如果每个链接中都有完整的url:
function isexternal($url) {
// Test if link is internal/external
if(stristr($url, "myDomain.com") || strpos($url,"/") == '0')
return true;
else
return false;
}
使用主机并将其与本地主机进行比较(通常但不总是与$\u服务器['HTTP\u host']
相同)
这将把www.example.com和example.com视为不同的主机。如果希望将所有子域视为本地链接,则函数将稍大一些:
function isexternal($url) {
$components = parse_url($url);
if ( empty($components['host']) ) return false; // we will treat url like '/relative.php' as relative
if ( strcasecmp($components['host'], 'example.com') === 0 ) return false; // url host looks exactly like the local host
return strrpos(strtolower($components['host']), '.example.com') !== strlen($components['host']) - strlen('.example.com'); // check if the url host is a subdomain
}
我知道这篇文章很旧,但我现在就在这里编写了我的函数。也许其他人也需要它
function IsResourceLocal($url){
if( empty( $url ) ){ return false; }
$urlParsed = parse_url( $url );
$host = $urlParsed['host'];
if( empty( $host ) ){
/* maybe we have a relative link like: /wp-content/uploads/image.jpg */
/* add absolute path to begin and check if file exists */
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$maybefile = $doc_root.$url;
/* Check if file exists */
$fileexists = file_exists ( $maybefile );
if( $fileexists ){
/* maybe you want to convert to full url? */
return true;
}
}
/* strip www. if exists */
$host = str_replace('www.','',$host);
$thishost = $_SERVER['HTTP_HOST'];
/* strip www. if exists */
$thishost = str_replace('www.','',$thishost);
if( $host == $thishost ){
return true;
}
return false;
}
这就是您可以简单地检测外部URL的方法:
$url = 'https://my-domain.com/demo/';
$domain = 'my-domain.com';
$internal = (
false !== stripos( $url, '//' . $domain ) || // include "//my-domain.com" and "http://my-domain.com"
stripos( $url, '.' . $domain ) || // include subdomains, like "www.my-domain.com". DANGEROUS (see below)!
(
0 !== strpos( $url, '//' ) && // exclude protocol relative URLs, like "//example.com"
0 === strpos( $url, '/' ) // include root-relative URLs, like "/demo"
)
);
上述检查将www.my-domain.com
和my-domain.com
视为“内部”
为什么这条规则很危险:
子域逻辑引入了一个可能被利用的弱点:例如,当外部URL在路径中包含您的域时,https://external.com/www.my-domain.com
被视为内部的
更安全的代码:
通过删除子域支持(我建议这样做),可以消除此问题:
外部的还是内部的?这个网页是从哪个网站上刮下来的?我想这就是他的意思。比如“这是一个跨网站链接吗?”假设有一个网站:xyz.com。。。如果它将一个链接链接到另一个网站abc.com,则该链接应标记为外部链接。但是,如果它使用同一个域(xyz.com)或本地URL,那么它不应该属于外部,因为您填写了$html变量,这意味着您知道域名,对吗?是的。我也有每一个链接及其href+域名。我只是想知道如何将其与相同的域URL分开。如果a[href]以/开头,请将其标记为内部。请更新代码。事实上,如果它以“/”开头,它是绝对的。“相对”是指html所在的位置。”例如,img/img1.png可以表示“/shared/img/img1.png”。是的,它是被添加的。线程开启器说它将包含该域@阿波罗:他想知道哪些是外部的;那样做是可以的。路径是绝对的还是相对的并不重要,只要它在同一个域空间中。是的。我不在乎。只要域包含测试域的主机名,就将其标记为内部。这意味着,如果正在测试abc.com,请将每个不带http且不包含abc.com的域以及每个带有域名的链接标记为非外部,反之亦然。@MehulMohan
//evilhost.example.com
以/
开头,在许多上下文中作为url有效。。。不要盲目地认为/
保证绝对的、相同的主机路径!不确定这是否是无故障的,如果有些链接包含www,有些则不是。如果www.(或其他子域)位于主机值中,则应将其删除。子域的检查不可靠。e、 g.example.com将与example.com匹配,它将把www.ihackeddomainname.com与www.domainname.com同等对待。除了strpos($url,“/”)==='0'将永远不会匹配错误答案,因为上面提到的原因^url字符串是相对链接怎么办?回答错误如果url包含您的url作为参数,例如:这将在您的function@RuslanBes,strpos($url,“/”)===“0”将永远不会匹配
这对于根相对url(相对于当前域)是正确的。但是,它也会误解协议相关URL,例如//example.com/page
,如果您使用web框架,您还应该检查这是否是有效的本地路由,如“/2017/10/some nice post”。但通常,框架已经有了这样的方法。顺便说一句,str_替换('www.,'','host')代码>只有在开始时才需要执行
function IsResourceLocal($url){
if( empty( $url ) ){ return false; }
$urlParsed = parse_url( $url );
$host = $urlParsed['host'];
if( empty( $host ) ){
/* maybe we have a relative link like: /wp-content/uploads/image.jpg */
/* add absolute path to begin and check if file exists */
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$maybefile = $doc_root.$url;
/* Check if file exists */
$fileexists = file_exists ( $maybefile );
if( $fileexists ){
/* maybe you want to convert to full url? */
return true;
}
}
/* strip www. if exists */
$host = str_replace('www.','',$host);
$thishost = $_SERVER['HTTP_HOST'];
/* strip www. if exists */
$thishost = str_replace('www.','',$thishost);
if( $host == $thishost ){
return true;
}
return false;
}
$url = 'https://my-domain.com/demo/';
$domain = 'my-domain.com';
$internal = (
false !== stripos( $url, '//' . $domain ) || // include "//my-domain.com" and "http://my-domain.com"
stripos( $url, '.' . $domain ) || // include subdomains, like "www.my-domain.com". DANGEROUS (see below)!
(
0 !== strpos( $url, '//' ) && // exclude protocol relative URLs, like "//example.com"
0 === strpos( $url, '/' ) // include root-relative URLs, like "/demo"
)
);
$url = 'https://my-domain.com/demo/';
$domain = 'my-domain.com';
$internal = (
false !== stripos( $url, '//' . $domain ) || // include "//my-domain.com" and "http://my-domain.com"
(
0 !== strpos( $url, '//' ) && // exclude protocol relative URLs, like "//example.com"
0 === strpos( $url, '/' ) // include root-relative URLs, like "/demo"
)
);