Archive for the ‘Coding’ Category

How does return i++ work?

Sunday, December 13th, 2009

This came up while working on some code. I want to keep an internal counter within a function, and every time the function is called, return the incremented result. The function ended with return $i++; which, after I typed it, realized may not work as expected.

I wrote this quick test code to figure out what might happen.

<?php
function increment()
{
	static $count = 0;
	// do stuff
	return $count++;
}
 
for($i=0; $i<4; $i++)
{
	echo 'i=' . $i . ' return=' . increment() . "n";
}
?>

I figured one of three things might happen:

Option 1: $count is never incremented because return returns the pre-incremented value and prevents it from incrementing.

   i=0 return=0
   i=1 return=0
   i=2 return=0
   i=3 return=0

Option 2: $count is incremented and the incremented value is passed to return.

   i=0 return=1
   i=1 return=2
   i=2 return=3
   i=3 return=4

Option 3: The current value of $count is evaluated and passed to the return, $count is incremented, then the function returns.

   i=0 return=0
   i=1 return=1
   i=2 return=2
   i=3 return=3

Well it turns out that the function does what I wanted it to do, option 3. Can you explain why the value of $count can be incremented after the function returns?

Google Analytics leaks source code?

Thursday, November 12th, 2009

While signing up for a new Google Analytics profile, we noticed a change in their new platform. The confirmation screen that gives you the tracking code is now drawn in via AJAX. Being the curious types that we are, we opened up Firebug to take a look at why this might be happening.

The first thing we noticed was that the response from Google looks like a block of PHP code wrapped in what appears to be a Java serialized string.

Google Analytics signup response

Cleaning up the response, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
  // Copyright 2009 Google Inc. All Rights Reserved.
  $GA_ACCOUNT = "MO-11576892-1";
  $GA_PIXEL = "ga.php";
 
  function googleAnalyticsGetImageUrl() {
    global $GA_ACCOUNT, $GA_PIXEL;
    $url = "";
    $url .= $GA_PIXEL . "?";
    $url .= "utmac=" . $GA_ACCOUNT;
    $url .= "&utmn=" . rand(0, 0x7fffffff);
    $referer = $_SERVER["HTTP_REFERER"];
    $query = $_SERVER["QUERY_STRING"];
    $path = $_SERVER["REQUEST_URI"];
    if (empty($referer)) {
      $referer = "-";
    }
    $url .= "&utmr=" . urlencode($referer);
    if (!empty($path)) {
      $url .= "&utmp=" . urlencode($path);
    }
    $url .= "&guid=ON";
    return $url;
  }
?>
<?php
  $googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
?>
<img src="<?= $googleAnalyticsImageUrl ?>" />

It’s definitely PHP, so why does the server return PHP code to a client-side script? It returned a function that builds up an image URL containing our account ID and some other information.

The Tracking Code Troubleshooting page has documentation on what all the query string parameters mean. The image URL that is built up contains only basic information of what would otherwise look like a normal tracking code request. Only the account ID, path, referrer, and a random number are passed. There’s also guid=ON, which is not in the documentation.

The other strange part is that normally the account ID begins with UA-, but in this case begins with MO-. Maybe it’s some sort of initialization request?