Just write this code down:
I am not a php developer and I don't have test environment set up to test. Just test if it is working or not. You can always remove that code if it is not.
You can put that code in session start so that it doesn't get executed on each page request.