PHP if statement works in template files, but not header

Because you ending php file with : and not putting at the end of header endif; php expects it to finish your IF.

In other words – you can not use this in header this way without ending IF. Or you have to create global variable and asign value [lets say true if logged in] to it in header so other php in theme can use it, or you have to use this code in every php you want to restric something:

if(is_user_logged_in()) {
your code for logged in visitors
} else {

your code for guests

}