How to check if customer is logged in or not in magento 2?

In Magento 2 we can check Customer is logged in or not by below way,
Using Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct method.

<?php

namespace Rbj\Customer\Block;

class Customerinfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
    }

    /*
     * return bool
     */
    public function getLogin() {
        return $this->httpContext->isLoggedIn();
    }
?>

Call getLogin function in template file,

$isLoggedin = $block->getLogin();
if($isLoggedin) {
    // show your message
}

Using ObjectManager, Never use Objectmanager directly in real code in magento 2. Here I have show just demonstrate using Objectmanager. Using Objectmanager directly create performance issue for the site.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userContext = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn) {
// do coding for customer loggin
}

 

How to get customer data by customer email in magento 2?

You can get customer information by just passing customer email using below code snippet, Create Block file,

<?php

namespace Rbj\Customer\Block;

class Customer extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        array $data = []
    ) {
        $this->customerRepository = $customerRepository;
        parent::__construct($context, $data);
    }
    /*
    * $email string
    * $websiteId int
    */
    public function getCustomerByEmail($email,$websiteId = null)
    {
        return $this->customerRepository->get($email,$websiteId);
    }

Call getCustomerByEmail() function in template file,

$customerEmail = 'roni_cost@example.com';
$websiteId = 1; // pass website id
$customer = $block->getCustomerByEmail($customerEmail,$websiteId);
echo $customer->getFirstname();
echo $customer->getEmail();
echo $customer->getLastname();

Get all customer data by below way,
echo “<pre>”;print_r($customer->__toArray());

For, Get customer data by customer id using below link, https://www.rakeshjesadiya.com/how-to-get-customer-data-by-customer-id-magento-2/

How to get remote address in magento 2 way?

Get Remote address in magento 2 by below code snippet,

<?php
public function __construct(
    \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
    ) {
    $this->_remoteAddress = $remoteAddress;
}
public function getRemoteAddress(){
    $remoteAddress = $this->_remoteAddress->getRemoteAddress();
    return $remoteAddress;
}

Call function in template file by below way,

echo $this->getRemoteAddress();