loading
Please wait while loading...

Read more PHP Geo

A nice library to calculate geographical distances for PHP.

example code:

use Location\Coordinate;
use Location\Distance\Haversine;

$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

echo $coordinate1->getDistance($coordinate2, new Haversine()); // returns 128384.515 (meters; ≈128 kilometers)
...........

Read more Post request show 404 not found on php with IIS

May related to:
1.
This line in my web.config file was causing the issue:
<trace enabled="true" localOnly="false" pageOutput="false" requestLimit="40" />

2.
I had this same problem. Any PHP POST to another PHP page was hanging. In the process of rebuilding the web.config file from scratch, I found an error message that suggested running the AppPool Managed pipeline mode in Classic mode.
After making the above change, my PHP code is working as expected.

 

 

Reference:
http://stackoverflow.com/questions/4357636/iis7-php-http-post-hang

Read more A nice way to output csv with temporary file

In php programming, we always need to output data with csv format, fputcsv is good function to do that. However, it require a file handler which means you must do fopen to open a file write the content to it. But usually we just need to generate the output file for download and don't need to save in server. Today I find a better way to write csv with php memory as shown below.

// output up to 5MB is kept in memory, if it becomes bigger
// it will automatically be written to a temporary file
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');

fputcsv($csv, array('blah','blah'));

rewind($csv);

// put it all in a variable
$output = stream_get_contents($csv);

Read more Google Oauth 2.0 ask permission every time

I have made a application using google account for login. Reference to google's example, it's easy make the login work well. However, I'm face to a serious problem that google ask for permission approval every time. For a openid login, like facebook, we will ask permission in the first time only and for the next time, it should automatically complete the login and no need to approve by user again. For this purpose, I search from the internet and finally found the solution as following:

To make the application just ask permission for once, add the following line in the config

$client->setApprovalPrompt(auto);
...........

Read more Verify email address by PHP filter_var

To check an email address is valid or not, we always use preg_match. Today I found that php5 has already allow a function to do so, which is filter_var

filter_var($email, FILTER_VALIDATE_EMAIL)

However, most checking script include filter_var doesn't well checking the domain part. For example, an email xxx@xxx.com1 can pass the validation but .com1 is a invalid domain name, so I add some code to the function to imporve the function as below.

function verifyEmail($email) {
	if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
		return preg_match('/^([a-z]{2,4})$/', end(explode('.',$email)));
	}
	return false;
}

Note: I have set 2-4 letter in the last part of the domain, but you may know there exists some special domain will not pass the validation (such as .travel, .museum)

filter_var also allow some more checking function, such as FILTER_VALIDATE_URL, FILTER_VALIDATE_IP, you may read detail on php.net

1 2