It's very simple actually. If you are going to use PHP, you can simply use the referral option.
Using this feature of PHP: $_SERVER["HTTP_REFERER"], you can see which site the visitor came from.
If the visitor came from the site of companyQ, then set a function to change the logo, but if the visitor came from anywhere else, then just leave the logo at a default.
I hope this made sense
