You can use
www.google-analytics.com put the code on your site and it will show you where you get refered from, such as Yahoo! Google Search and the keywords used, and other websites. It is very useful. I am sure though you could use PHP to determine that but it would be easier with a Stats tracker, Such as I said, because a PHP script that your asking about may be resource intensive
