Thursday, November 12, 2009

AJAX and PHP sessions

One thing that trips up AJAX development is how to persist your PHP session through your AJAX calls. In a browser, the session persists through a cookie that passes the session id upon each request. However with an AJAX call, the browser cookies are not passed and the session is lost.

There are a couple of easy ways around this. First, if your AJAX library supports cookies, you can just set the PHP session id cookie before the call. If you don't have cookie support, then the following "manual" method works quite well.

What you will want to do is pass the session id as a POST or GET variable in your AJAX request. We'll call our variable name "sid".

Here is a jQuery example for passing the sid through as a POST value (in a PHP script):

$.post("test.php", { sid: "<?php echo session_id(); ?>" } );

This will POST the current session id through the form. Next on the PHP side, you must set the session id with this posted value:


<?php
// set session to value from ajax post
session_id($_POST['sid']);
// we now have $_SESSION data!
?>

As for security, be aware that you don't have the same restrictions that come with cookies (domain, date, etc.) so once you pass the session id to javascript, be careful what can be manipulated through javascript code. It's pretty much the same as being aware of javascript getting/setting cookie values themselves.

Hope that helps!

Saturday, October 24, 2009

Being a winning online poker player

You know you are capable of playing great poker, but you continuously lose your bankroll playing online poker. How do you get over this hurdle? What is the secret? There are two important factors.

Factor 1: Don't play on tilt

Winning poker is not about focusing on how much you've won or lost. It's how you handle losing. How often do you get bad beat, then jump right back in and lose more and more? How often do you make a big win, only to lose it all over the course of days or weeks?

So to overcome factor 1: If you get a bad beat or get on a losing streak, force yourself to take a break before playing again. How long or what this is depends on what it takes to get your mind free of the past. Maybe it's a nap. Maybe it's a walk around the house. Maybe it's a jog around the block. The important element is that you are not playing the next game with the predisposition of steaming. Get over it first, then continue playing.

Factor 2: Bankroll management

Losing is a fact of the game, it will happen. To stay alive, you must manage your bankroll. This sounds easy, but it can take a considerable amount of discipline. If you don't want to buy in again, you *must* manage your bankroll, no matter how good you are. A rule of thumb for sit-and-goes and MTTs: Take the buy-in multiplied by # of players. If that amount is greater than your bankroll, move down.

If you get on a losing streak, just stick to the plan. Take a break, and move down in stakes until you can recover.


Follow these rules, and the only way you can lose is by playing bad poker. Hopefully you've gotten past that part :) Good Luck!

Sunday, August 9, 2009

Basecamp PHP API

I needed a complete PHP class for the Basecamp API with a project I'm working on at REBEL. Finding nothing that would fit the bill, I wrote one.

http://code.google.com/p/basecamp-php-api/

Sunday, July 26, 2009

A rant on PHP, quotes and variables

Here are 3 ways to display some text with some PHP variables:

single quotes interspersed with vars:

echo 'My name is ' . $user
. ' and I eat ' . $food['favorite'] . '.';


double quotes interspersed with vars:

echo "My name is " . $user
. " and I eat " . $food['favorite'] . ".";


double quotes with embedded vars:

echo "My name is {$user} and I eat {$food['favorite']}.";

The vast majority of PHP code I come across uses the first two methods, breaking in and out of quotes for each variable. IMHO the third way much easier to read and maintain. Granted there are slight performance differences between them, but not enough to be concerned about. I think the reason that version 3 isn't so popular is because most PHP developers don't learn this type of curly-brace syntax (enabling you to embed complex variables into quoted strings) right away, and therefore rarely think to use it.

Of course you can take things a step further with sprintf and really keep things tidy. Let's take a SQL query for a common example.

Here is the "messy" way:

$sql = "select * from MYTABLE where id=" . (int)$info['id'] . " and firstname = '" . mysql_real_escape_string($info['firstname']) . "' limit " . (int)$limit;

And the much cleaner way:

$sql = sprintf("select *
from MYTABLE
where id=%d
and firstname = '%s'
limit %d",
(int)$info['id'],
mysql_real_escape_string($info['firstname']),
(int)$limit
);


I hope I brought some ideas to light, happy coding.

Thursday, May 14, 2009

Camera image orientation

I was having a confusing problem where images taken from the iPhone camera were being displayed in the wrong orientation. I would take a photo in portrait and save the data to disk. Later when I retrieved the photo it would show up rotated 90 degrees.

The iPhone knows what orientation a photo is taken, and this orientation depends on how the camera is being held when you snap the photo. You can get the orientation value on a UIImage from the property imageOrientation.

When you save the photo, it is possible that this information could get lost, depending on the way you save it. If it does get lost, the UIImageView will assume the photo was taken as a left-side landscape photo, and display it with that orientation.

I ran into a problem when trying to save the camera image with UIImagePNGRepresentation(). For some reason this loses the orientation info. I switched to UIImageJPEGRepresentation() and all is well now.

So basically if you run into the problem, you have 2 choices: Be sure you use a format that preserves orientation data, or rotate the UIImage data directly before saving it to a file.

Wednesday, May 13, 2009

Rotating view around arbitrary point the easy way

I have a UIImageView that I want to rotate around a given point. By default, the layer will rotate around the center of the UIImageView. Trying to change this anchor point involves a bit of work, either move at the beginning and reposition all the view elements, or translating the view, rotating, and translating back. The last option works fine, unless you are trying to use core animation to smoothly rotate the view.

I found out a much easier solution to the whole problem. You could call it a workaround, but it works well without disturbing the view you want rotated.

Basically, you create a new UIView. Place the center point where you want the rotation to occur. Change the size of the view so that the view you want rotated will fit inside. Make your view (you want rotated) a subview of the new view. Now, just rotate the new view, and your subview will be rotated correctly.

Example: lets say we have a UIImageView that is 320x480 (the size of the whole iPhone screen). We want it to rotate from the bottom center instead of its middle. Create a new UIView that is 0,0,320,960. This is basically double the height of the screen, positioned so the bottom half is off the screen, which places the center point right where we want it. Now place your UIImageView inside the new UIView in the top half (visible on the screen.) Now when you rotate the UIView, it will pivot on the bottom center of the screen, and your UIImageView will rotate along with it, as expected.

Friday, May 1, 2009

OS X "burning" image to USB flash drive

I recently downloaded an Ubuntu image for netbooks, and I needed to install this onto a USB flash drive so I could boot it. This is pretty simple with OS X and Disk Utility and dd from the command line. Be sure your flash drive is large enough to hold the disk image.

* Plug the flash drive into a USB port and launch Disk Utility.
* Select the flash drive and press apple+i to bring up the info
* remember the device id, it will be diskN where N is a number, such as disk4
* select the volume (not the device) and click unmount.
* open a terminal window, and type:


sudo dd if=/path/to/file.img of=/dev/disk4 bs=1024


Be sure to use your path to the image file, and the device id you got from disk utility in place of "disk4". You can drag the .img file into the terminal window if you don't want to type the full path to it.

Now wait a bit for the image to be written. When its done you will get something similar to this:


969568+0 records in
969568+0 records out
992837632 bytes transferred in 521.666371 secs (1903204 bytes/sec)