0

I need to find out if a page is returning a 404 or 301 status code. I know I can write a php script to use cURL to return a value for javascript to read, but I am trying to simplify the process and kinda new. Right now I have an onBlur function that makes sure the webpage is at least in the correct format before they leave the field. But I would like it to also check the status of the page and I can't seem to find a solution for using cUrl directly with javascript or any examples of how this would be done. Anybody care to help me out please? Here is my validate.js that I am calling on the page...

function loadXMLDoc() {
    var url = document.getElementById("webpage_url").value;
    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("valid").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "custom/modules/CT221_SEP_test/js/curltest.php/" + url, true);
    xmlhttp.send();
}

function isURL() {
    var url = document.getElementById("webpage_url");
    var urlerr = document.getElementById("url_error");
    var reg = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
    if (!reg.test(url.value)) {
        urlerr.innerHTML = "Invalid WebPage Address";
        url.focus();
    } else {
        loadXMLDoc();
        urlerr.innerHTML = //calls success image";
    }
}
YAHOO.util.Event.on("webpage_url", "blur", isURL);

then my curltest.php file looks like this...

$url = $_GET['url'];

function validateurl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    curl_close($ch);
    preg_match("/HTTP\/1\.[1|0]\s(\d{3})/", $data, $matches);
    return $matches[1];
}
$code = validateurl($url);
if ($code != "404") {
    echo "Webpage is valid";
} else[
echo "Webpage is not live";
}
1

1 Answer 1

0

Sounds like a simple case of creating a PHP proxy script. The function that validates the URL could (on success) call an AJAX function to a PHP scrip, passing the URL so that you can make a cURL request to it. Instead of pinging the website directly, you could opt use some sort of DNS lookup. Upon your PHP script finishing, you can return some sort of value/data to the AJAX function which can act accordingly on the page. Chances are you will need to use callbacks on the client and server side so as not to hang the page, unless that is preferable to them moving on with the rest of the form. Your call.

Sign up to request clarification or add additional context in comments.

7 Comments

I have a php script that calls curl and returns the status code, but I'm not sure what to do from there. or how to even call that php script with an onBlur function.
do you know how to setup a callback with AJAX? You look into some tutorials. Basically, you need to determine if that status code is useful to you or not. Basically, if it's anything other than 200, you could echo/'return' SUCCESS, else you would echo/'return' FAILURE. This would be returned to your page as part of the XmlHttp Requests responseText property. Based on the value of responseText, you can make something happen on the page. The basics can be found here: w3schools.com/ajax/default.asp
ok, I am more lost than ever, is there any way you could give me a quick example? I have a php script that calls cRUL and checks the page and returns the status code as return $matches[1]; but how do I get that variable and then use it to do something?
I've already given you the overview and a link. You need to use AJAX. AJAX is a technique for making a request with Javascript. You can make a request to the script that is making the cURL request and pass it the URL via GET or POST. the script executes, and returns a status code. you echo the code or and the response is returned to the function making the AJAX request, located in .responseText. Then you can do something based on the response on the page. I would suggest a basic Google search for AJAX, and reply with some code attempts so we can offer you more help.
these comments are limiting my character count so I can't post my code that I already have, that isn't working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.