简体   繁体   English

检查链接是否存在

[英]Check if link exists

I have a phpBB forum on my localhost for learning purposes..now I'm trying to do this using PHP : 我在我的localhost上有一个phpBB论坛用于学习目的..现在我正在尝试使用PHP来做到这一点:

When a link gets posted,a script checks if the exact link exists and if it does then the process of posting the message doesn't continue. 发布链接时,脚本会检查是否存在确切的链接,如果存在,则不会继续发布消息的过程。 EDIT: This is what I have in includes/message_parser.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);}}

And this is what I have in posting.php 这就是我在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..';
}

It works. 有用。 I can see it echoing what da hell when I post a link that exists,but the problem is that the post gets posted. 当我发布一个存在的链接时,我可以看到它回应了什么地狱,但问题是帖子被发布了。 what I want now is,if the link exists,then don't allow the post to be posted. 我现在想要的是,如果链接存在,那么不允许发布帖子。

2ND EDIT: 2ND编辑:

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

That's what I did to prevent submitting the topic when the url exists. 这就是我在网址存在时阻止提交主题的做法。 not working :\\ 不工作:

Checking if link return status code 200 (dunno about 30x) 检查链接返回状态代码是否为200(dunno约为30x)

Using cURL: 使用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 :(";
}

Without cURL: 没有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 :(";
}

You can play around with it :) 你可以玩它:)

UPDATE UPDATE

To the updated question. 更新的问题。

I don't know exactly how phpBB works or where you're trying to catch it, but some suggestions to stop the posting might be to check for the link using javascript/jQuery and then disable the submit button alerting/printing a statement as to why the post wasn't posted. 我不确切知道phpBB是如何工作的或者你想要捕获它的地方,但是一些停止发布的建议可能是使用javascript / jQuery检查链接然后禁用提交按钮警告/打印声明为什么帖子没有张贴。

I'm not that into regex and such, but you would check the post if it contains any link you where looking for, and then "block" the submit of the form. 我不是那样的正则表达式,但你会检查帖子,如果它包含你寻找的任何链接,然后“阻止”表单的提交。

Something along the lines of: 有点像:

$('#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;
    }

});

as extra security, you could disable the submit by default, and only enable it once the javascript has loaded. 作为额外的安全性,您可以默认禁用提交,并且只有在加载javascript后才启用它。

First check to see whether the link format is valid: 首先检查链接格式是否有效:

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

Then make sure the resource that the link refers to actually exists: 然后确保链接引用的资源实际存在:

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

try: 尝试:

$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