It seems that you will need to add your script to the top of each page in order for this to work the way you need it to work. Can't you use the same script you have for redirecting to the right cms, to redirect the user to the correct page?
Please let me know if i misunderstood your question.