Get current category id from post page wordpress

WordPress has a function wp_get_referer, which gets the referring url from the http-header. So, if you go from a category archive to a single post, the name of the category will normally be included in that url. This means you can start your single.php like this:

$refer = wp_get_referer();
if (strpos($refer, 'category/cat1') != false) {... do something ...}
elseif (strpos($refer, 'category/cat2') != false) {... do something else ...}
else {... do a default thing ...}

Depending on your setup this solution may need some tweaking, but the general idea is clear, I hope.