PHP for loops with multiple statements in each expression

PHP for loops work in the same way as other programming languages and have a useful feature where each expression between the semi-colons can have multiple statements to be executed when separated by a comma. I found this useful myself when I need to have both a zero based and 1 based index in a loop.

Categories PHP

PHP: get keywords from search engine referer url

This post shows how to use PHP to extract the keywords searched on by a user when they found your website using a seach engine. Bing, Google and Yahoo are covered here and you can easily add your own to the PHP code supplied.

Categories PHP

Google Analytics API PHP Class – Updates May 22nd 2009

I’ve made some updates to my Google Analytics API PHP Class today and posted the updated class and example files. The changes are documented here.





auth property is now public

The auth property which contains the authorization key after you’ve logged in is now a public property. You could therefore now do this:

$api1 = analytics_api();
 $api1->login($email, $password);
 
 $api2 = analytics_api();
 $api2->auth = $api1->auth;
 

You could also save the auth key into a database and use it for subsequent requests; once authorized it’s valid for several days and this can cut down initial request time.

data method start and end parameters

I’ve added the ability to specify the values “today”, “yesterday” and “week” as the $start parameter when calling the data method. “week” sets the start and end dates to be the 7 days before today. Leaving them as false defaults to the month before today (i.e. the same behaviour as previously).

filters parameter added to the data method

The $filters parameter has been added to the data method for specifying filters. When a value is passed in for this parameter it adds “&filters=$filters” to the request string. I haven’t used filters yet myself with the exception of a couple of tests when adding this option.

debug parameter added to the data method

If you set debug to true when calling data the url that is used to call the Analytics API is echoed out to standard output which is useful for seeing what’s going on behind the scenes. If run from the CLI will echo it along with a linebreak; otherwise will put it in a <p> tag and end with a newline.

get_summary method added

A new method get_summary was added to get summary information for a profile: number of visits, number of pageviews, average time on site and pages per visit. I’ll post more about this new function tomorrow.

get_summaries method added

Works the same as the above but gets summaries for all profiles you have access to.

sec2hms method added

This function is from http://www.laughing-buddha.net/jon/php/sec2hms/ and is used to format the seconds returned in the get_summary method into h:m:s format.

Categories PHP

PHP email validation with filter_var – updated

Just over a year ago I posted how to validate email addresses with PHP using filter_var instead of having to mess around with regular expressions. As pointed out in a comment, chris@example will pass validation; while that is actually a valid email address (the domain part of an email address doesn’t actually have to have dots in it, e.g. localhost) in most real situations we don’t want it to validate so this post adds a regular expression to the test to ensure the part after the @ contains a dot.

Categories PHP