March 3, 2013

Secure JavaScript Site Specific API

Question by Ryan Naddy

I have been thinking about how to do this for a while, and I am not sure what the best approach is for this.

What I want to do, is allow users to sign up their website, they then are given a javascript snippet with a key, code, and a link to a file stored on my server.

Example

<script>
    var prefix_key  = 1234567890;
    var prefix_code = "hfj48fj4587tgfj5trutjh47dl4gx04jd9f";
</script>
<script src="http://mysite.com/js/somefile.js"></script>

Now, what can happen is say I have that on my site, and you come and copy it to your site. How can I secure it so it doesn’t work on your site but still works on mine?

Note: I will be using PHP as a back end if back end is needed.

Answer by Starx

Get the key and code from an ajax request based on a fixed API code which is user specific.

For example:

Give you every user a fixed API code to query the page which returns key and code

http://mysite.com/some_page_to_get_the_key_and_code.php?apikey=243h325h2353
March 31, 2012

How to calculate driven distance using latitude & longitude in php?

Question by hazem

Given something like this:

//$unit="K";
//$unit="m";

$driven_distance ($lat1, $lng1, $lat2, $lng2, $unit);

How I can I get the time between these two points?

Answer by Starx

A pick out function [Source]

function getDistance($latitude1, $longitude1,
$latitude2, $longitude2, $unit = 'Mi')
{
   $theta = $longitude1 - $longitude2;
   $distance = (sin(deg2rad($latitude1)) *
   sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
   cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
   $distance = acos($distance);
   $distance = rad2deg($distance);
   $distance = $distance * 60 * 1.1515;
   switch($unit)
   {
      case 'm': break;
      case 'K' : $distance = $distance *1.609344;
   }
   return (round($distance,2));
}

This functions rounds the result to two decimal places.

Now use exatly the way you are using

$driven_distance = getDistance($lat1,$lng1,$lat2,$lng2,$unit);

Update:

If you want to find out the driven distance the best bet is to use google’s geocode api.

$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=true_or_false
');
$details = json_decode($json, TRUE);
var_dump($details);
August 16, 2010

can we pass $_POST value by clicking link?

Question by Giffary

I need to build some php API to upload an image cross subdomain.

Example, upload script located at http://sub.domain.com/upload.php

and I neew to upload an image to http://www.domain.com/media/

I read some content at http://www.v-nessa.net/2009/02/09/simple-api-writing-part-i and i suspect that can we pass $_POST value by clicking link? How?

Answer by Starx

Yes, you can do that… by submitting a form while clicking that link

<a href="#" onclick="document.myformname.submit();">Submit</a>
...

Please fill the form - I will response as fast as I can!