Have you customized the database or admin login page at all? When you login to the admin, it basically autoselects your first gallery. It seems that is not happening in your case for some reason. In the admin/index.php line 47-54 is what sets the default gallery, so you could do some debugging in that area to see if its returning the correct database record.
Assuming that looks like it has the correct info, you could try adding session_write_close() just before each header call a few lines below.