I am inserting a snippet at the top of my WordPress page to test whether a user has logged in. I want to redirect the user if the user has not logged in. I created the following snippet. I can call it from the editor so it appears to function. I get no errors but do not get a redirection or an error when not logged in. I presume I am not seeing the error due to being in Wordpress and am guessing I cannot do this as parts of the page have already been written and I am not redirecting at the top of the code before anything has been written. Does anyone have a workaround for redirecting the user? Or maybe I just need to find a plugin to test for login?
<?php
if(isset($_SESSION['sessionId'])) {
echo "you are logged in";
} else {
header ("location: index.php?error=Mustlogin");
}