I can't give you the PHP code straight up but I've done this in ASP.
Create a table in your db with Coupon Codes and Values.
Insert a field on your checkout page for Coupon Code.
Create a discount in the eCart that applies only if the Coupon Code matches one in the db.
ie: if Coupon Code matches, then do Discount Calculation