我试图部署flash文件嵌入html到谷歌应用程序引擎。Flash(动作脚本2.0)使用"post"方法发送主机名,并通过php函数gethostbyname()获取其ip地址。事实上,我知道谷歌应用引擎不支持php。所以我尝试使用另一种方式来部署ipPHP.php在其他免费的web服务器,只有flash文件在谷歌应用程序引擎。但它不工作,我不知道为什么。你能给我一个解决这个问题的建议吗?
-------------- domaintoip。佛罗里达州 ---------------------
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");
}
?>
如果您的站点托管在应用引擎上,由于同源策略,您不能对应用引擎以外的主机进行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);
}