2010/06/18

http://ajax.googleapis.com is down

Many sites are hosting their javascript libraries on googleapis.com, such as jquery, which is supposed to be faster because googleapis is CDN (Content Distribution Network), however, what happen if googleapis.com is down.

Today, I see this problem, I went to nettus, slickdeals, they are extremely slow, I see this at the Firefox process bar,

connecting to ajax.googleapis.com...

Initially, I thought it is my ISP problem, I could not open “ajax.googleapis.com” at all. Then I tried http://downforeveryoneorjustme.com/, which told me, the site looks down for everyone not just for me.

I also went to Twitter to see if people are talking about this, unfortunately, their site also uses googleapis, and the search seems broken.

Anyone have the same problem?

By the time (1:55pmEST, 06-18-2010) I am writing this, the googleapis.com looks down for me for about 30 mins.

Updated: 2:00pm EST 06-18-2010, looks like the server is back. Not sure it is just my ISP problem, because ajax.googleapis.com’s home page is a not found page (BTW, I got timeout error when there was a problem), downforeveryoneorjustme.com may always say that it looks down.

2010/05/26

mysql get month from UNIX TIME

Try to count total registers in a certain month from members table, but the regdate is in UNIX TIME STAMP. Here is a solution using MYSQL's function FROM_UNIXTIME

SELECT count( * ) FROM `members` WHERE month( FROM_UNIXTIME( `regdate` ) ) =12

2010/04/09

PHP get ip function

A pretty good way to get IP, especially, your site has different servers, i.e. load balancing.

 function get_ip(){
    if (isset($_GET['ip'])) {
       $ip = stripslashes($_GET['ip']);
    } elseif (isset($_SERVER) and !empty($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else {
            $ip = getenv('REMOTE_ADDR');
        }
    }
    return $ip;
}

2010/03/19

smarty dollar to cent

In a project we have a smarty variable presenting a dollar value like ‘1.23’, or ‘.06’. And we wanted to show cent value when the value less than one dollar, for exmaple,

  1. “.78” will be “78”
  2. “.06” will be “6”

Instead of crating a new smarty variable in php, here is a workaround I made in the template.

{*here $dollar_per is in dollar value*}
{if $dollar_per && $dollar_per < 1}
   {if $dollar_per < 0.1} {*ex. 0.06*}
   {assign var="cent_per" value=$dollar_per|substr:2} {*get 6*}
   {assign var="cent_per" value="&nbsp;"|cat:$cent_per} {*add space before 6 in my case*}
   {else} {*ex. 0.78*}
   {assign var="cent_per" value=$dollar_per|substr:1} {*get 78*}
   {/if}
{/if}

Note: substr and cat were used!

2010/03/12

name attribute of <a> tag

There are several attributes of <a> tag, and ‘name’ is one of them. I just found today, that you cannot have character, ‘%’, as a value of this attribute.

I used to use name attribute to hold some data, then pass it as variable in jQuery code as the following,

$(document).ready(function() {
  $(".s-img>a").hover(function(){
    var largePath = $(this).attr("name"); 
    // note: here I used 'name' attr 
    $("#limg").attr({ src: largePath});
    return false;
    },
    function() {
      ;
  });
});

It failed when the name has ‘%’ character in there. Fortunately, the “alt” attribute is happy with ‘%’as its value. So then, changed it ‘alt’, it works.

P.S. Actually, I see most of ‘alt’ other than ‘name’ to pass variable. Maybe, this is a reason.

2010/03/11

ajax setcookie failed with page reload

I have ajax to set cookie and then reload the page, the original code is some thing like the following:

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set"); //set cookie
  location.reload(true); //reload the page
});

But it failed. Looked around and figured out that the .get ajax called might not be successful before the page reload. ajax is asynchronous, so the reload function could be executing during the ajax called.

The solution is using ajax callback to make sure reload function runs after ajax call ended, see the revise code as the following,

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set", function(){
    location.reload(true);
  });
});

2010/01/26

informix database using PHP

In a project, I need to write a php code to talk to informix database, which I have not done before.

First, you need PHP Informix Module, here is an guide, http://www.ibm.com/developerworks/data/library/techarticle/dm-0606bombardier/

Fortunately, the server I am working on has already installed this module, so I just wrote a simple test script as the following,

<?php
$conn_id = ifx_connect ("mydb@ol_srv1", "user", "pass");
?>

This code snippet is from PHP Manual, which is actually not a good for test. because if there is some error in ifx_connect, you will not see any error message. I found a better one from PhpDig.net, http://www.phpdig.net/ref/rn30re552.html

if (!$conn = @ifx_connect( "stores7@demo_se", "testuser", "password" )) {
   die( sprintf( "Connection error: %s %s", ifx_error(), ifx_errormsg() ) );
}

I experienced the following error at the beginning,

Connection error: E [SQLSTATE=IX 001  SQLCODE=-1829] Unable to load locale categories.

The guy from IBM had an explanation http://www.iiug.org/forums/ids/index.cgi/noframes/read/3333

OK, some environment parameters missing, $INFORMIXDIR, after fixing this one, I got another one missing:

Connection error: E [SQLSTATE=IX 000  SQLCODE=-25560] Environment variable INFORMIXSERVER must be set.

Finally, fixed!