简体   繁体   English

添加http或https以进行用户输入验证

[英]Add http or https for user input validation

$xml = $_GET['url']

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

..
..

if the user put without http or https my script will be broken, is concatenation a good way to validation in this case? 如果用户输入的内容不带http或https,那么我的脚本将被破坏,在这种情况下,串联是验证的好方法吗?

The simplest way of doing this is checking for the presence of http:// or https:// at the beginning of the string. 最简单的方法是检查字符串开头是否存在http://https://

if (preg_match('/^http(s)?:\/\//', $xml, $matches) === 1) {
    if ($matches[1] === 's') {
        // it's https
    } else {
        // it's http
    }
} else {
    // there is neither http nor https at the beginning
}

You are using a get method. 您正在使用get方法。 Or this is done by AJAX, or the user appends a url in the querystring You are not posting a form? 还是通过AJAX完成此操作,或者用户在querystring附加了网址?您不是要发布表单吗?

Concatenation isn't going to cut it, when the url is faulty. 如果网址有问题,则串联不会删除它。 You need to check for this. 您需要检查一下。

You can put an input with placeholder on the page, to "force" the user to use http:// . 您可以在页面上输入带占位符的输入,以“强制”用户使用http:// This should be the way to go in HTML5. 这应该是在HTML5中使用的方式。

 <input type="text" pattern="^(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$" placeholder="http://" title="URLs need to be proceeded by http:// or https://" >

This should check and forgive some errors. 这应该检查并原谅一些错误。 If an url isn't up to spec this will return an error, as it should. 如果网址不符合规范,则会返回一个错误,这应该是正确的。 The user should revise his url. 用户应修改其网址。

$xml = $_GET['url']

$xmlDoc = new DOMDocument();
if (!preg_match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, $xml ) )
{
    echo 'This url is not valid.';
    exit;
}
else if (!preg_match(/^http(s)?:\/\/, $xml))
{
    //no http present
    $orgUrl = $xml;
    $xml = "http://".$orgUrl; 
    //extended to cope with https://
    $loaded = loadXML();
    if (substr($loaded, 0, 5) == "false")
    {
        //this attempt failed.
        $xml = "https://".$orgUrl;
        $loaded = loadXML();
        if (substr($loaded, 0, 5) == "false")
        {
             echo substr($loaded, 6);
             exit;
        }

    }
}
else
{  
    $loaded = loadXML();
}

function loadXML()
{
  try {
     return $xmlDoc->load($xml);
  }
  catch($Ex)
  {
     return echo 'false Your url could\'t be retrieved. Are you sure you\'ve entered it correctly?';
  }
}

You can also use curl to check the url before loading xml: 您还可以在加载xml之前使用curl来检查url:

$ch = curl_init($xml);

// Send request
curl_exec($ch);

// Check for errors and display the error message
if($errno = curl_errno($ch)) {
    $error_message = curl_strerror($errno);
    echo "$error_message :: while loading url";
}

// Close the handle
curl_close($ch);

Important side-note : Using this methods to check if the url is available and than take the appropriate action can take a very long time, since the server response can take a while to return. 重要的旁注 :使用此方法检查url是否可用,然后采取适当的措施可能会花费很长时间,因为服务器响应可能需要一段时间才能返回。

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

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