I'm still missing a lot of essential details about the authentication mechanismes used in both sites, but I'm going to make some general suggestions.
I assume that you are using some kind of session based authentication mechanisme, that sets some session variables when a user is succesfully authenticated. I also assume that both databases contain the same user/password data and you have some way to keep these synchronized.
This is what you could do:
- When somebody logs in succesfully on site A, you redirect them to a script running on site B, supplying the login credentials. E.g http://www.siteb.com/login_from_site_a.php?username=yourusername&password=secret
- In the script login_from_site_a.php you proces the supplied credentials to login the user on site B. After processing you redirect the user back to site A.
- Apply the same system to allow users to login on site A from site B.
You could make it more fancy by using a POST instead of a GET call to the login_from_site_a.php script to avoid passing credentials in the URL location bar. Please note that this login_from_site_a.php script does not produce any output, it should always end in a redirect to site A.