php - 包含 javascript 时出现错误 403 Forbidden

标签 php javascript ajax

我有几段代码应该从另一个网站获取表单。 header 中的 php 会清理 URL 中的 GET 字符串。 基本上我有一个页面:order.html,如果我将整个 javascript 函数粘贴到其中,它就可以工作:

<?php  if (isset($_GET['url'])) {
            echo file_get_contents("http://".sanitizeString($_GET['url']));
        }
function sanitizeString($var) {
    $var = strip_tags($var);
    $var = htmlentities($var);
    return stripslashes($var);
} ?>


<html>
<body>
<p>html text still displays</p>
<script>
document.write("<div id='info'></div>");
nocache = "&nocache=" + Math.random() * 1000000
request = new ajaxRequest()
request.open("GET", "<?php echo basename($_SERVER['SCRIPT_FILENAME']); ?>?url=platform.leedhub.com/merchants/form.php?merchant_id=<?php echo $_GET['merchantid']; ?>&margin=<?php echo $_GET['affiliateid'] ?>&padding=<?php echo $_GET['URL'] ?>" + nocache, true);
request.onreadystatechange = function()
{
    if (this.readyState == 4)
    {
        if (this.status == 200)
        {
            if (this.responseText != null)
            {
                document.getElementById('info').innerHTML =
                this.responseText
            }
            else alert("Ajax error: No data received")
        }
        else alert( "Ajax error: " + this.statusText)
    }
}
request.send(null)
    function ajaxRequest()
    {
        try
        {
            var request = new XMLHttpRequest()
        }
        catch(e1)
        {
            try
            {
                request = new ActiveXObject("Msxml2.XMLHTTP")
            }
            catch(e2)
            {
                try
                {
                    request = new ActiveXObject("Microsoft.XMLHTTP")
                }
                catch(e3)
                {
                    request = false
                }
            }
        }
        return request
    }
</script>
</body>
</html>

但是,如果我从外部 URL 获取编写的 javascript 并包含它,它将不起作用。

<?php  if (isset($_GET['url'])) {
echo file_get_contents("http://".sanitizeString($_GET['url']));
}
function sanitizeString($var) {
$var = strip_tags($var);
$var = htmlentities($var);
return stripslashes($var);
} ?>
<p>html text still displays</p>
<html>
<body>
<script src="urlget.js"></script>
</body>
</html>

我收到 403 禁止错误。它从 request.send(null) 告诉我。

访问被禁止 您无权访问所请求的对象。它要么是读保护的,要么是服务器无法读取的。

你知道我该如何解决这个问题吗?我需要将该脚本作为外部文件,以便我可以将其包含在多个网站上。

最佳答案

我认为这是一个文件系统权限问题。您必须确保 Web 服务器具有 urlget.js 文件的读取访问权限。您必须找出哪个用户正在运行 Web 服务器的工作线程,并授予该用户对 urlget.js 文件的读取权限。

关于php - 包含 javascript 时出现错误 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567286/

相关文章:

php - 从 php 代码中禁用文本框

php - 将数组值显示为数组中的数字

php - 将大型本地数据库与服务器数据库 (MySQL) 同步

javascript - 如何检查 Gatsby 中的前题是否为空

javascript - <JSP/JQuery> 如何制作点击按钮时的文本输入框

javascript - YoutubeData API V3 - 错误 400 : required parameter - part

php - 高效使用mysql Table缓存复杂查询

javascript - 如何在 Element UI Input v2.x 中键入时发出值?

javascript - jQuery AJAX 自定义函数和自定义回调?

php - 在 AJAX 调用中传递多个参数