Easy, if you have SSI enabled on your server.
Say your gallery page is
http://www.greatporn.com/galleries/gallery1.html and the link you want your affiliate code on is
http://www.greatporn.com/paysite/index.cgi?12345 (where 12345 is your affiliates code).
First change your link on your gallery to
http://www.greatporn.com/paysite/index.cgi?<!--#echo var="QUERY_STRING"-->
Then just get your affiliates to append their code to the link to your gallery thus:
http://www.greatporn.com/galleries/gallery1.html?12345
Basically <!--#echo var="QUERY_STRING"--> means "paste whatever data was sent with the URL here". However note that your server must be set to take Server Side Includes (SSI) and most servers insist that you use the extension .shtml instead of .html on the page (IE
http://www.greatporn.com/galleries/gallery1.shtml not
http://www.greatporn.com/galleries/gallery1.html), but the rules vary on different servers so you will have to ask your host about this.