HELP ME! HTML form validate URL exists on submit!

Discussion in 'Programming' started by ElusiveOne, Feb 7, 2011.

  1. #1
    I have been looking everywhere and I can't sort this!

    I have an existing html form asking for a pre-existing subdomain.
    I need the subdomain input field to be checked on submission and halted if it does NOT exist!

    eg:

    User Inputs Subdomain in field of form
    Code validates that hxxp://www.domain.com/Subdomain exisits
    If it exists the form is sent
    If it doesn't the user is asked to enter a valid subdomain!

    Please help anyone?
    What PHP or javascript should I include to check the validity of a URL on submission of this form like the email validation does!
     
    ElusiveOne, Feb 7, 2011 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK, your example given is a subdomain but a sub folder.

    Is the domain your domain or a remote domain?
    By it "existing" do you mean that it has a webpage there?
     
    AstarothSolutions, Feb 7, 2011 IP
  3. ElusiveOne

    ElusiveOne Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am trying to confirm if subfolder exists at http://www.xboxgamertag.com/search/{gamertag}
    Because if it does then the gamertag is valid. if the code returns an error then I can assume that the text inputed to the form field is NOT a valid gamertag.

    I only want to submit the form if the gamertag entered is valid.

    I can also do this by checking if a file exists at
    http://avatar.xboxlive.com/avatar/{gamertag}/avatar-body.png

    If it does then again the gamertag is valid. if the code returns an error in that the file is not reachable then I can assume that the text inputed to the form field is NOT a valid gamertag.

    How can I validate the inputted text in the form field to one of these items??
    I can then either pass the form through or tell the user to enter a valid gamertag

    Hope you get me!
     
    ElusiveOne, Feb 7, 2011 IP
  4. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You will need to do it server side rather than client side.

    Would be a simple case of a short script to send a request for the headers at the URL and check for a 200 response coming back. I don't do PHP but the following seems to be the basis for it:
    http://php.net/manual/en/function.get-headers.php
     
    AstarothSolutions, Feb 8, 2011 IP
  5. ElusiveOne

    ElusiveOne Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have found a way of this to work...

    <?php
    function remoteFileExists($url) {
    $curl = curl_init($url);

    //don't fetch the actual page, you only want to check the connection is ok
    curl_setopt($curl, CURLOPT_NOBODY, true);

    //do request
    $result = curl_exec($curl);

    $ret = false;

    //if request did not fail
    if ($result !== false) {
    //if request was ok, check response code
    $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ($statusCode == 200) {
    $ret = true;
    }
    }

    curl_close($curl);

    return $ret;
    }
    $exists = remoteFileExists('http://www.xboxgamertag.com/search/gamertag');

    if ($exists) {
    echo 'Gamertag VALID Please complete form';
    } else {
    echo 'Gamertag INVALID Please check and try again';
    }

    ?>

    All I need to do now is replace the word 'gamertag' for the string in my form which is $userlogin
    I know its basic but how do I place the string in this arguement.
    I have tried {$userlogin} .$user_login. 'echo $user_login' and various other things.
    I have also tried replaceing the whole url 'http://www.xboxgamertag.com/search/gamertag' with a $testurl
    and had a $testurl = 'http://www.xboxgamertag.com/search/gamertag' before it but the remoteFileExists arguement doesnt like it and keeps returning as valid

    Any advice?
     
    ElusiveOne, Feb 8, 2011 IP
  6. ElusiveOne

    ElusiveOne Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The lower half should be:

    $link = "http://www.xboxgamertag.com/search/{$user_login}";
    $exists = remoteFileExists($link);

    if ($exists) {
    echo "<font color='green'>Gamertag <b>VALID</b> Please complete form</font>";
    } else {
    echo "<font color='red'>Gamertag <b>INVALID</b> Please check and try again</font>";
    }

    This works great! Trial and error, hope this helps someone else!
     
    ElusiveOne, Feb 8, 2011 IP