Dec 25, 2009

Quick Tip: light switch effect

Here's a quick tip for novice programmers. Every time you want to create a "light-switch" effect for toggling a parameter on and off, or should I say true or false, 0 or 1, ... You can simply do it by typing:
p ^= 1;
This is a binary-safe Exclusive-OR assignment operator. First, it computes the value of p XOR 1 (p = p ^ 1) and then stores that value in p. If we look at the truth table of exclusive-or (XOR)

a

b

a XOR b

0

0

0

0

1

1

1

0

1

1

1

0

We see that if our b variable is 1 (true) the result of a ^ 1 (a XOR 1) is exactly the opposite of a. All in all, this operation is simply a shortcut for these assignments:
p = p ^ 1;

p = !p;

p = p ? false : true;

if(p) p = false; else p = true;

So if you want to toggle something ON and OFF, let's say when a user clicks a button, this method is extremely short and useful.

Dec 20, 2009

Facebook Connect: FB.Connect.streamPublish() does not show up

DUE TO FREQUENT FACEBOOK API CHANGES THIS ARTICLE IS OUTDATED.

Here are the reasons why FBJS functions sometimes fail:
  • xd_reciever.html is not installed correctly
  • application key is not set in FB.init()
  • Facebook Connect callback URL is not set in the application settings
  • Facebook JavaScript API is not set correctly in your HTML. This should be set immediately after the body tag <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script> This should be set before the closing body tag <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
  • Facebook functions are called before JavaScript objects are initialized. FB.init(api_key, channel_path); FB.ensureInit(function() { // put your functions here });
  • All Facebook JavaScript functions are called BEFORE the FeatureLoader.js.php You should put your functions after this call and into FB.ensureInit() function
  • There are JavaScript errors in your code
If any of the errors mentioned above are met, the function will fail. If FBML tags are not rendered, you might also want to check, if you set the HTML header correctly. http://wiki.developers.facebook.com/index.php/XFBML Also, XFBML tags are required to render tags correctly.

Sep 17, 2009

PHP: mail() Sender domain must exist - UPDATE

Regarding my last post, it seems that you can't set the Return-Path in the header parameter of the mail() function. The workaround is in the fifth argument of mail() - the additional parameters. If you set the "-fsender@domain.com" it should set the Return-Path. Here's my mail() wrapper function:
function construct_mail($to,$subject,$content,$sender='') {
$from = ADMIN_EMAIL;
if(!empty($sender)) $from = $sender;
$header  = "From: $from\r\n";
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Date: ".date("r")."\r\n";
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "X-Mailer: PHP\r\n";
$content = ''.$content.'';
$subject = ' =?UTF-8?B?'. base64_encode($subject) ."?=";
return mail($to,$subject,$content,$header,"-f$from");
}
Also, the -f switch might trigger a E_WARNING if you don't set the trusted users (the user that executes the script - webserver) in the /etc/mail/trusted-users

UPDATE: http://dev.kafol.net/2013/01/sendmail-x-authentication-warning-user.html

PHP: mail() Sender domain must exist

Lately I've found out that a lot of emails sent from PHP's mail() get bounced (they weren't before). Servers bounce the message with the "Sender domain must exist" error, or more precisely, the domain in question was the servers (the server that the script runs on, not the email server) hostname. Well of course that domain doesn't exist (localhost.localdomain), so after some googling, I figured that some additional headers might help, like Return-Path and Reply-To.
$from = "your@email.com";
$header  = "From: $from\r\n";
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Date: ".date("r")."\r\n";
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "X-Mailer: PHP\r\n";
I'll test it now, when this post reaches my newsletter subscribers.

UPDATE: http://dev.kafol.net/2009/09/php-mail-sender-domain-must-exist.html

UPDATE #2: http://dev.kafol.net/2013/01/sendmail-x-authentication-warning-user.html

Aug 3, 2009

PHP: calculating age with no bugs

As it turns out, calculating a person's age is not a very simple thing to do.

I've been very lazy lately so I googled for a PHP or MySQL function to calculate age in years. The more I searched, the more I found out that people were doing it wrong.

For example: floor(time_difference_in_days) / 356, let's say 7000/356 = 19,66.

What the hell? That won't work. We have leap years and such.

And even worse: some calculated the difference in days with strtotime() or mktime() that uses unix timestamps, which is very unfortunate for people born before 1970-01-01. (if you don't know why, read the wiki)

I decided to do this the way that we humans do it. It's commented and streched out for a reason. Stop doing it wrong.
function age($dob) {
//year, month, day of birth
 list($y, $m, $d) = explode('-', $dob);
//this year,month, day
 list($yn, $mn, $dn) = explode('-', date('Y-m-d'));

//AGE is (THIS_YEAR - YEAR_OF_BIRTH) if the person already had or is having his birthday THIS_YEAR
 $age = $yn - $y;

//If the person didn't have his birthday yet, then AGE is (THIS_YEAR - YEAR_OF_BIRTH - 1)
 if ($mn < $m) {
//THIS_MONTH is less than MONTH_OF_BIRTH so no birtday yet this year!    
  $age--;
 } else {
//if it's the same MONTH_OF_YEAR then the birthday did not occur only if THIS_DAY_OF_MONTH is less than DAY_OF_BIRTH    
  if ($mn == $m) {
   if ($dn < $d) {
    $age--;
   }
  }
 }
 return $age;
}