# check if user is logged in
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn())
{
$request = $objectManager->get('Magento\Framework\App\Request\Http');
//get instance for URL interface
/** @var \Magento\Framework\UrlInterface $urlInterface */
$urlInterface = $objectManager->get('Magento\Framework\UrlInterface');
// URL to redirect to
$url = $urlInterface->getUrl('customer/account/login');
if(strpos($request->getPathInfo(), '/customer/account/') !== 0)
{
# redirect to /customer/account/login
header('Location:'.$url );die();
}
}
Please do static content deploy after placing this code in your phtml.
sudo php bin/magento setup:static-content:deploy