Solution to Broken Cookie Creation for Chrome on Linux

Friday, February 19th, 2010

After receiving an update today to push my Chrome version to 5.0.322.2 dev, I noticed that I could no longer log in to the web site that I had open when the update was done. I happened to be developing a website when I received notification for the update so, like always, I just continued developing and let the new version of Chrome install in the background.

When it was finished, I closed all my browser windows, re-opened Chrome and navigated back to the site I had open that I was working on. Try as I might, I couldn’t seem to get my $_SESSION information working for PHP. This was first noticeable when I tried to log in and it didn’t appear to work correctly. I tried another web site and it worked fine. I tried another browser on my development site and that worked fine too. I decided to go ahead and use the session_id() function and noticed that I was getting a new session ID with every page load.

I finally did some digging around and realized Chrome wasn’t setting any cookies for the domain. I tried other websites and they all worked fine. For some reason, the site I had open when the browser upgraded just wasn’t creating cookies for me.

I checked my configuration settings. I cleared all my personal information. In the “Under the Hood” menu in the Google Chrome options, I set my cookies to “Accept All” and still, no luck. Cookies were working just fine everywhere else except for where I needed to do work.

Frustrated, I decided to click the “Reset to defaults” button and it worked!

So, if you’re having troubles getting Chrome to create cookies for just one domain, try resetting defaults in your “Under the Hood” menu and see if that works for you.

I hope this helps others experiencing this annoying problem.

If you liked this post, then please consider subscribing to my feed.

Random Dates and Numbers in PostgreSQL with the RANDOM() Function

Wednesday, January 20th, 2010

Working with the random() function in PostgreSQL can be a bit tricky if you’re trying to populate a table.

The random() function in PostgreSQL will return a number between 0 and 1 like so:

SELECT RANDOM();
      random       
-------------------
 0.115072432027698
(1 row)

If you’re trying to get a whole number from random(), you can use some multiplication and the round() function to let random() work for you. Say you wanted to get a random number from 0-100:

SELECT ROUND(RANDOM() * 100);
 round 
-------
    22
(1 row)

For the project I’m working on, we wanted to pre-populate some birthdays with random dates. I tried using a combination of the datetime functions with an interval and random() and couldn’t quite get there. Searching around on Google didn’t provide too many useful results so I turned to the wonderful folks in the #postgresql chat at irc.freednode.net. Using a combination of the above and the suggestions from the chat room, I was able to come up with a query that did what I wanted:

SELECT NOW() - '1 year'::INTERVAL * ROUND(RANDOM() * 100);
           ?COLUMN?           
------------------------------
 1987-01-20 11:10:34.26494-07
(1 row)

That means we could easily update our tables for people with random birthdays with a single update query:

UPDATE table_name SET birthday = NOW() - '1 year'::INTERVAL * ROUND(RANDOM() * 100);

Hopefully you found this post useful. If you did, please consider subscribing to my feed.

Speed Comparison of strlen() VS empty() in PHP Shows empty() is Faster

Thursday, October 8th, 2009

While working on a PHP project, I found myself occasionally switching between using empty() or !strlen() when I was dealing with strings. In PHP, there are often multiple solutions for the same problem and this was one of those cases. I didn’t really have a reason to use one over the other and, as most PHP developers know, you should be careful when using empty() for strings because you can run into this problem:

<?php
    // Here's a string that isn't "empty", it's zero
    $string = '0';
    if (empty($string)) {
        echo 'Oops!';
    }
?>
 
Oops!

Being that I felt capable of being careful with empty(), I wanted to know if there was any speed advantage to using it over strlen(). I wrote a little function and was very surprised by the results:

Checking "if (!strlen($string))" VS "if (empty($string))"

unset($string);
Average speed gain using empty() instead of strlen(): 84.62%

$string === false;
Average speed gain using empty() instead of strlen(): 57.25%

$string = '';
Average speed gain using empty() instead of strlen(): 47.02%

Repeating the test yields similar results every time. Keep in mind, we’re talking fractions of seconds here, but it all adds up. I was expecting there to be a much smaller difference in speed between the two.

Here’s the test I wrote. It’s nothing spectacular and there may be a better way to do it but it gets the job done:

<?php
function test_strlen_vs_empty($string) {
    /* We occasionally have oddities so I wanted to make them observable and count them properly so our avg is accurate */
 
    $oddities = 0;
 
    /* We want to be able to test an unset variable */
    if ($string == 'unset') {
        unset($string);
    }   
 
    /* We'll do 100 tests */
    for ($i = 0; $i < 100; $i++) {
        /* Start our first timer */
        $start_time = microtime(true);
 
        /* We want to check each function 100 times */
        for ($ii = 0; $ii < 100; $ii++) {
            /* Check if the string is empty() */
            if (empty($string)) {
            }   
        }
        /* End our first timer */
        $end_time = microtime(true);
 
        /* Calculate how long it took */
        $time_for_empty = $end_time - $start_time;
 
        /* Start our second timer */
        $start_time = microtime(true);
        for ($ii = 0; $ii < 100; $ii++) {
            /* Check if strlen() returns something equal to false */
            if (!strlen($string)) {
            }   
        }
 
        /* End our second timer */
        $end_time = microtime(true);
 
        /* Calculate how long it took */
        $time_for_strlen = $end_time - $start_time;
 
        /* If strlen() was faster (which doesn't happen often) we want to record it */
        if ($time_for_strlen < $time_for_empty) {
            $oddities++;
        } else {
            /* Otherwise, it's as normal. We want to grab a nice percentage of how much faster empty was than strlen */
            $slowers_total += round(100 - (($time_for_empty / $time_for_strlen) * 100), 2); 
        }   
    }   
    /* Output the results with an average taking into account any oddities that may have occurred */
    echo "\n" . 'Average speed gain using empty() instead of strlen(): ' . round($slowers_total / (100 - $oddities), 2) . '%';
    if ($oddities) {
        echo "\n" . 'Number of times where strlen() was actually faster: ' . $oddities;
    }   
}
 
echo "\n\n" . 'Checking "if (!strlen($string))" VS "if (empty($string))"' . "\n\n";
echo 'unset($string);';
test_strlen_vs_empty('unset');
echo "\n\n" . '$string === false;';
test_strlen_vs_empty(false);
echo "\n\n" . '$string = \'\';';
test_strlen_vs_empty('');
?>

This works by calculating how long it takes to run a check on the string 100 times using each function. It compares the times, and works out how much faster one is over the other in a percentage. This is performed 100 times. At the end, each of the 100 percentages are averaged out to discover how much faster empty() was over strlen(). We check this for three different instances of of empty strings:

  1. When the $string variable is not set
  2. When the $string variable is set to false
  3. When the $string variable is set to a string with no characters (”)

If you look at the code, you’ll see I added handling for “oddities.” Occasionally, strlen() would actually perform faster than empty(). It was random and didn’t happen on every test but it was something that I wanted to observe and properly handle so it’s in there.

So, you may be wondering, why empty() seems to be so much faster. Here is a simple explanation for you. empty() is faster…

“…because empty() is a language construct built into the Zend engine, while strlen is implemented as a standard extension function.

Conclusion: if you are careful enough with empty() you should probably try to use it over strlen() if you’re trying to squeeze all the speed you can out of your application.

If you liked this post, be sure to subscribe to my feed.