简体   繁体   中英

Check if link exists

I have a phpBB forum on my localhost for learning purposes..now I'm trying to do this using 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

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

$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:

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)

Using 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:

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

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.

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.

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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