I\'m really not a php expert and didn\'t write the code below but it\'s working fine on my website. I use this to redirect users if not logged when clicked on some links.