How can I detect the site the user came from before accessing mine in PHP?
As @Andrew Hare states in his answer, getting the value of the HTTP_REFERRER server value (which is a header that is sent as part of the HTTP request) will tell you the site that the browser was last on.
What should be noted, however, is that it is completely possible that this header/server variable will have no value, for a number of legitimate reasons, some being:
All of the above are really variations on the same thing, a case where the same browser window is used for going to another site, but wasn't prompted through clicking the on a link in a document which lead them there, a redirect, or some other action prompted by the page in the history before yours.