【问题标题】:google app engine problem of post method in flash and phpflash和php中post方法的google app engine问题
【发布时间】:2011-04-25 17:37:28
【问题描述】:

我正在尝试将嵌入在 html 中的 flash 文件部署到 google 应用引擎。 Flash(action script 2.0) 使用“post”方法发送主机名并通过 php 函数 gethostbyname() 获取其 IP 地址。 事实上,我知道 google app engine 不支持 php。 所以我尝试使用另一种方式将 ipPHP.php 部署在其他免费的网络服务器中,并且只在谷歌应用引擎中部署 flash 文件。 但它不起作用,我不知道为什么。 你能给我一个关于这个问题的提示吗?

-------------domaintoip.fla ---------

result_lv = new LoadVars();

result_lv.byname = _root.domainnm;

trace("Sending... " + result_lv.byname);

result_lv.onLoad = function (success)
{
    if (success)
    {
         _root.ip = unescape(this.result);
        trace("Return value from the PHP : " + unescape(this));

        if(_root.ip.length==5){
            _root.flag=1;
            }

        else{               


            var mystring=_root.ip;
            arr=mystring.split(".");

            _root.ipby1=arr[0];
            _root.ipby2=arr[1];
            _root.ipby3=arr[2];

            if(arr[3].length==15)
            {
            _root.ipby4=arr[3].substr(0,3);
            }
            if(arr[3].length==14)
            {
                _root.ipby4=arr[3].substr(0,2);
            }
            if(arr[3].length==13)
            {
                _root.ipby4=arr[3].substr(0,1);
            }
            _root.flag=0;

        }

    }
    else
    {
        trace("Cannot call the PHP file...");
        _root.flag=1;
    }
}
result_lv.sendAndLoad("http://anotherserver../ipPHP.php", result_lv, "POST");

------------- ipPHP.php ---------

  <?php

$Var1 = $_POST['byname'];

$rtnValue = gethostbyname(trim($Var1));

if(ip2long($rtnValue) == -1 || $rtnValue == $Var1 ) {
    $rtnValue =0;

echo (result=$rtnValue");
}
else {

echo("result=$rtnValue");

}

?>

【问题讨论】:

  • 为什么需要获取客户端的IP地址?

标签: php google-app-engine


【解决方案1】:

如果您的网站托管在应用引擎上,则由于 Same Origin Policy,您无法向应用引擎以外的主机进行 AJAX 调用。此限制通常是正确的,并且并非特定于应用程序引擎。概括地说,对于托管在域 X 的任何网页,该网页无法向域 Y 发出 AJAX 请求。

您实际上遇到了一个更根本的问题:当您拥有的唯一工具是锤子时,每个问题看起来都像钉子。实际上,您可以使用doPost 方法通过应用程序引擎轻松处理 POST 请求,并且您可以非常轻松地以与 PHP 脚本非常相似的方式获取客户端的 IP 地址。绝对没有理由在这里使用 PHP;您已经设置了一个全新的服务器来调用一个内置的 PHP 函数?这太疯狂了;您可以使用应用引擎 servlet 执行完全相同的操作。

考虑以下代码:

public void doPost(HttpServletRequest request,HttpServletResponse response) {
    /* get "byname" param, equivalent to $POST['byname'] */
    String rtnValue = request.getParameter("byname");

    /* TODO: your if statements and other logic */

    /* print response to client, equivalent to your echo statement */
    response.getWriter().print("result=" + rtnValue);
}

【讨论】:

  • 其实我对java和servlet的了解并不多:(我应该想办法用servlet解决。如果可以的话,能不能帮忙写代码?谢谢大家的帮助。
猜你喜欢
  • 2010-10-03
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 2013-10-28
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多