【问题标题】:Security with Google Maps Javascript APIGoogle Maps Javascript API 的安全性
【发布时间】:2018-09-25 04:29:35
【问题描述】:

我正在开发一个使用 PHP 和客户端 Javascript 的网络应用程序,其中实现了 Google Maps Javascript API。

此网络应用程序的目的是能够为旅程预订司机,例如 ....

为了计算旅程的距离,我使用了距离矩阵 API,但我问我如何确保回调不作弊?

查看我的代码

   service.getDistanceMatrix(
            {
                origins: [route[origin]],
                destinations: [route[destination]],
                travelMode: 'DRIVING',
                avoidTolls: true,
            }, callback());

        function callback(response, status) {
                var dist = response.rows["0"].elements["0"].distance.value;
                var duration = response.rows["0"].elements["0"].duration.value;
        }
);

在这里用户可以轻松更改“dist”变量的值!这将用于计算旅程的价格。
即使我通过 AJAX 调用直接将数据发送到服务器,他们也可以修改此调用的参数并设置他们想要的任何内容。

在这种情况下,我可能使用了错误的开发解决方案。
如果我使用了错误的解决方案,或者是否存在保护我的应用程序的方法,请告诉我。
提前感谢您的回答。

【问题讨论】:

    标签: javascript php security google-maps-api-3 client-side


    【解决方案1】:

    首先,用户可以随时编辑 Javascript。例如,创建该语言的目的是为网页制作动画、显示照片、视频或与服务器交互。它只提供了一种使网页动态化和动画化的方法,而不是所有商业方面。

    为了从用户那里得到正确的信息,你必须使用php。数据可靠,可以直接在服务器中使用。


    如果你想找到用户的位置,我可以在php中给你这段代码:

    <?php
    $user_ip = getenv('REMOTE_ADDR');
    $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
    $country = $geo["geoplugin_countryName"];
    $city = $geo["geoplugin_city"];
    ?>
    

    注意:这段代码很简单,但不是很准确。如果您想了解更多详细信息,可以参考 Stack Overflow 的this 页面。


    现在,如果您想估计两个位置之间的距离,请使用以下代码:

    function GetDrivingDistance($lat1, $lat2, $long1, $long2)
    {
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        curl_close($ch);
        $response_a = json_decode($response, true);
        $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
        $time = $response_a['rows'][0]['elements'][0]['duration']['text'];
    
        return array('distance' => $dist, 'time' => $time);
    }
    

    你可以这样使用这个函数:

    $coordinates1 = get_coordinates('Tychy', 'Jana Pawła II', 'Śląskie');
    $coordinates2 = get_coordinates($city, $country);
    if ( !$coordinates1 || !$coordinates2 )
    {
        echo 'Bad address.';
    }
    else
    {
        $dist = GetDrivingDistance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']);
        echo 'Distance: <b>'.$dist['distance'].'</b><br>Travel time duration: <b>'.$dist['time'].'</b>';
    }
    

    注意:我在 Stack Overflow 的 this 页面上找到此代码。

    如果您有任何问题或一些 cmets,请告诉我。

    【讨论】:

    • 非常感谢您的回答!这正是我想要的。如果我有任何问题,我会告诉你。
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2015-04-01
    • 1970-01-01
    • 2020-05-11
    • 2013-01-10
    • 2023-03-31
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多