Auto redirect after login success

//page – access require authenticate
if (!isset($_SESSION[‘userid’])) {
    $urlParam = ($_SERVER[‘REQUEST_URI’]!=’/’) ? ‘?return=’.base64_encode($_SERVER[‘REQUEST_URI’]) : ”;
    header(‘Location: /login.php’.$urlParam);
    die;
}
…

//login.php
…
if ($login_success) {
    $return = isset($_REQUEST[‘return’]) ? base64_decode($_REQUEST[‘return’]) : ”;
    if ($return) header(‘Location: $return’);
    else header(‘Location: default.php’);
}