If I had to make a guess without any information on how the User ID gets into the cart, I would have to say it is probably domain switching.
The User ID is probably a session, sessions are based on the domain cookie, what this means is if yoou log in at:
http://mydomain.com/login.php
then get redirected using the www address to the car page:
http://www.mydomain.com/cart.php
the login session is no longer set because the session browser cookie sees that as 2 separate domains.
you can prevent this by using htaccess to always force www or not:
http://www.htaccessbasics.com/force-www-nonwww-domain/