phpformscookiesinfusionsoft

Login Form Reappearing on Every Page


We have an infusionsoft login form modal on every page. When the user logs in, the modal is hidden and the information on the page is shown, and it stays shown on refresh. However, when the user navigates to another page, the login form reappears, even though the cookies from the login are still there. Here is the code of user-auth.php:

<?php

add_action( 'init', 'user_auth' );

/**
*
*
*/
function user_auth()
{
    if( !isset( $_POST['user-auth'] ) ) {
        return;
    }


    $page_id = 6;

    $code = get_field( 'access_code', $page_id );

    $v_username = isset( $_POST['entryemail'] ) ? $_POST['entryemail'] : '';
    $v_value = isset( $_POST['entrycode'] ) ? $_POST['entrycode'] : '';
    $v_time = isset( $_POST['remember'] ) ? $_POST['remember'] : 0;

    $infusion = new Infusion();
    $user = $infusion->findByEmail( $v_username );

    if( $user && ( $v_value == $code ) ) {

        $time = $v_time ? strtotime('+1 year') : strtotime('+3 day');

        $md5 = user_auth_user_save( $user );

        $url = parse_url( site_url() );
        $path = isset( $url['path'] ) ? $url['path'] : '';
        setcookie( 'website_login', $md5, $time, $path, $url['host'] );

        $GLOBALS['localpopup'] = true;
    }
    elseif( $user ) {
        $GLOBALS['localpopup'] = 'Invalid access code.';
    }
    else {
        $GLOBALS['localpopup'] = 'Your email cannot be found.';
    }
}

the form (login.php):

<?php

$email = isset( $_POST['entryemail'] ) ? $_POST['entryemail'] : '';
$code = isset( $_POST['entrycode'] ) ? $_POST['entrycode'] : '';
$remember = isset( $_POST['remember'] ) ? $_POST['remember'] : false;

$error = '';
if( isset( $GLOBALS['localpopup'] ) ) {
    $error = $GLOBALS['localpopup'];
}

?>
<div class="form-code-wraper" id="code-login">

    <span class="logo-popup"><img src="<?php echo THEMEURL ?>images/logo.png" alt="Pointy"/></span>

    <h1>Welcome</h1>
    <h2>Please enter your access code</h2>

    <form accept-charset="UTF-8" action="" class="entry-form" method="POST">
        <div class="input-holder-popup enter-code clearfix">
            <div class="input-row clearfix">
                <label for="entryemail">Email: </label>
                <input class="field-input-container" id="entryemail" name="entryemail" type="email" dir="ltr" required value="<?php echo $email ?>"/>
            </div>
            <div class="input-row clearfix">
                <label for="entrycode">Code: </label>
                <input class="field-input-container" id="entrycode" name="entrycode" type="text" required  value="<?php echo $code ?>"/>
            </div>
            <div class="input-row clear remember-input">
                <input type="checkbox" id="remember" name="remember" value="1" <?php if( $remember ) : ?> checked="checked"<?php endif ?>/> <label for="remember">Remember me</label>
            </div>
        </div>

        <div class="buttons send-button">
            <input type="submit" name="user-auth" value="Enter" id="user-auth" />
        </div>
    </form>

    <div class="send-info">
        <?php if( $error ) : ?><div class="error-message"><?php echo $error ?></div><?php endif ?>
        <a href="#" class="register-link">No code? Click here</a>
    </div>

</div>

popup.php:

<?php
if(is_page('282')){
    return;
}else {
    if( user_auth_popup_hide() ) {
        return;
    }
}
?>
<div id="light" class="white_content">
    <?php get_template_part( 'blocks/popup/login' ) ?>
    <?php get_template_part( 'blocks/popup/register' ) ?>
</div>
<div id="fade" class="black_overlay"></div>



function user_auth_popup_hide()
{
    $localpopup = isset( $GLOBALS['localpopup'] ) && ( $GLOBALS['localpopup'] === true );
    $cookie = isset( $_COOKIE['website_login'] );
    $special_page = in_array( get_the_ID(), array(
        169,
        172,
    ) );

    return $localpopup || $cookie || $special_page;
}

Solution

  • SOLVED: This was not a code issue but rather an issue of having caching installed that was blocking auth cookies.

    small bug in code.... instead of using

    $url = parse_url( site_url() ); $path = isset( $url['path'] ) ? $url['path'] : '';

    The cookie needs to be set with setcookie( 'website_login', $md5, $time, '/', 'websitedomainname.com' );

    I had tried '/' before as the path and it was not working, so the website domain name was essential in this case.