In order to prevent the direct access to site B, you are going to need to code your scripts on server B to refuse any visitors that haven't first visited site A.
As for finding a way to permit access to site B from site A, I can think of a couple of ways.
One would be to provide a secret token from site A that gets sent to site B through a form. Another way would be through a cookie. Site B would receive a form submission (GET or POST) from Site A that contained the token...or Site B would check for the presence of a cookie. The security of Site B would depend heavily on how secret your cookie is.
To further increase security, you might have Site A and Site B pass some information like "expect a visit from User X with pass word "mypassword"...or something like that.
hope this is helpful.