Redirect if the code is called from the wrong page
Thing is, that's going about it the wrong way. The links should simply be setup correctly and there will be no need for a workaround.
Basically what I am saying is to not create the problem instead of creating workarounds for the problem.
I'm writing another quick script related to this right now you can find it in the script portal I linked to above.