Alternative to drupal_goto() function?
The problem is not drupal_goto() but your usage of it in a node. THere is a long standing issue in Drupal core to deal with this but I honestly think doing this is just wrong.

Instead of putting it in a node, you should create a custom module and put it in a page callback. See What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc? why it's a bad idea in general to use PHP in nodes, blocks and so on. You've seen one of those yourself, namely that it can be extremely hard to find out where the problematic code is actually coming from.

That said, the last comment in the linked issue points to a Field redirection module that is however currently limited to user/node references and links. And it's not dynamic as your code is. But again, for something like that you really should be creating a custom module.