繁体   English   中英

检查链接是否存在

[英]Check if link exists

我在我的localhost上有一个phpBB论坛用于学习目的..现在我正在尝试使用PHP来做到这一点:

发布链接时,脚本会检查是否存在确切的链接,如果存在,则不会继续发布消息的过程。 编辑:这是我在includes / message_parser.php中的内容

function url_exists($url) {
$handle = @fopen($url, "r");
if ($handle === false){
return false;
fclose($handle);}
else{
return true;
fclose($handle);}}

这就是我在posts.php中所拥有的

$your_url = "http://www.somewhere.com/index.php";

$your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

if (url_exists($your_url))
{
echo 'yeah, its reachable';
}
else
{
echo 'what da hell..';
}

有用。 当我发布一个存在的链接时,我可以看到它回应了什么地狱,但问题是帖子被发布了。 我现在想要的是,如果链接存在,那么不允许发布帖子。

2ND编辑:

if($submit){
    $URL = "http://www.fileserve.com/file/rP59FZ2";
    preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $url);
if(url_exists($url)) {
    echo "Link exists!";
}

这就是我在网址存在时阻止提交主题的做法。 不工作:

检查链接返回状态代码是否为200(dunno约为30x)

使用cURL:

function url_exists($url) {
    $ch = @curl_init($url);
    @curl_setopt($ch, CURLOPT_HEADER, TRUE);
    @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $status = array();
    preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($ch) , $status);
     return ($status[1] == 200);
}
// now call the function
$myUrl = "http://www.somewhere.com";
if(url_exists($myUrl)) {
    echo "Link exists!";
} else {
    echo "Link does not exist :(";
}

没有cURL:

function url_exists($url) {
    $h = get_headers($url);
    $status = array();
    preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
    return ($status[1] == 200);
}
// now call the function
$myUrl = "http://www.somewhere.com";
if(url_exists($myUrl)) {
    echo "Link exists!";
} else {
    echo "Link does not exist :(";
}

你可以玩它:)

UPDATE

更新的问题。

我不确切知道phpBB是如何工作的或者你想要捕获它的地方,但是一些停止发布的建议可能是使用javascript / jQuery检查链接然后禁用提交按钮警告/打印声明为什么帖子没有张贴。

我不是那样的正则表达式,但你会检查帖子,如果它包含你寻找的任何链接,然后“阻止”表单的提交。

有点像:

$('#myform').submit(function(event){
    // disable the submit button so the user doesn't spam it
    $('#myform input[type=submit]', this).attr('disabled', 'disabled');

    // check with regex for the links
    // if it's found return true else return false to variable preventSubmit
    if(preventSubmit) {
        // prevent the form from being submitted
        event.preventDefault();
        // notify the user
        alert("You cannot post that link!");
        return false;
    }

});

作为额外的安全性,您可以默认禁用提交,并且只有在加载javascript后才启用它。

首先检查链接格式是否有效:

function isValidURL($url) {
  return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

然后确保链接引用的资源实际存在:

function checkLink() {
   $handle = fopen("http://www.example.com/", "r");
   if ($handle) { fclose($handle); return true; }
   return false;
}

尝试:

$url = 'http://www.anyURL.com';
$hndl = @fopen($url,'r');
if($hndl !== false){
   echo 'URL Exists';
}
else
{
   echo "URL Doesn't exist";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM