• I’m using Ninja forms and am trying to make a plugin where they have to log into an account so they can register for events. I have the page with a form that asks for their user name and password. When they submit the form, I check their creds and if everything is okay, they get redirected to the same page where I have code that is hooked into the init that is suppose to set some cookies and then redirects. It is redirecting but I can’t get the cookies to set. I’ve been at this for about four hours now and can’t figure out what I’m doing wrong. Any help is appreciated!

    add_shortcode('bbr_account', 'bbr_account_func');
     function bbr_account_func() {
      return var_export($_COOKIE['account_id'],true);
     }
    
     if(!is_admin()) {
     Global $wp;
    $dslug = $_SERVER['REQUEST_URI'];
      if($dslug=='/dpi-sign-in/') {
    
       add_action('init', 'bbr_login');
      }
     }
    
    function bbr_login() {
     Global $wpdb;
     $uip = $_SERVER['REMOTE_ADDR'];
     $login_info = $wpdb->get_row('SELECT * FROM         '.$wpdb->prefix.'bbr_login WHERE ip LIKE "'.$uip.'"', ARRAY_A);
    if(!is_null($login_info)) {
     $user_info = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'bbr_usrs WHERE id = '.$login_info['id'], ARRAY_A);
        $wpdb->delete($wpdb->prefix.'bbr_login',array('id'=>$login_info['id']));
    
     setcookie('account_id',$user_info['id'],time()+3600,'/','www.register.dynamicpathwaysinc.com');
     header('refresh: 4; url=/account');
     }

    When it gets redirected, I get Notice: Undefined index: account_id in /home/dpiadmim/register.dynamicpathwaysinc.com/wp-content/plugins/ninja-forms-dpi-registration-system/ninja-forms-dpi-registration-system.php on line 17 NULL

Viewing 14 replies - 1 through 14 (of 14 total)
  • you cannot set cookies outside the domain where the php code is running.
    If I’m right, it creates a setcookie code outside the wordpres context, that is, it creates a php code where you set a cookie for the external domain and from the external domain checks if that cookie exists.

    Thread Starter brad3260

    (@brad3260)

    What do you mean by external domain? I’m setting the cookie for the same domain that the code is running. I’ve even tried omitting the domain and path.

    Do you have any piece of code before what you have posted here?

    Thread Starter brad3260

    (@brad3260)

    No echo or print statements or any lines that send anything to the browser

    Moderator bcworkz

    (@bcworkz)

    You may not have sent anything, but WP could have. Without more context it’s hard to say. set_cookie() should be called around the point when “send_headers” action fires. Alternately, use JavaScript to set cookies. Then you can do so at any time.

    setcookie('account_id',$user_info['id'],time()+3600,'/','www.domain.com');
    $_COOKIE will be created after the next interaction.

    add_shortcode('bbr_account', 'bbr_account_func');
    function bbr_account_func() {
    if(isset($_COOKIE['account_id'])){
    return ((int) $_COOKIE['account_id'])
    }
    return false;
    }

    Cast int for account_id or false on fail

    Dion

    (@diondesigns)

    Try setting the cookie using the base domain, as follows:

    setcookie('account_id',$user_info['id'],time()+3600,'/','.dynamicpathwaysinc.com');
    

    Also, if your PHP script is setting the cookie and then trying to read it later in the script, it will not work. The newly-set cookie is not available until the redirected page is loaded.

    <?php
    error_reporting(-1);
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    //add_shortcode('bbr_account', 'bbr_account_func');
    $user_info['id'] = 2;
    bbr_account_func();
    function bbr_account_func() {
    //if(isset($_COOKIE['account_id'])){
    global $user_info;
    return ((int) $_COOKIE['account_id']);
    /*}
    return false;*/
    }
    /*if(!is_admin()) {
    Global $wp;
    $dslug = $_SERVER['REQUEST_URI'];
    if($dslug=='/dpi-sign-in/') {
    add_action('init', 'bbr_login');
    }
    }*/
    bbr_login();
    function bbr_login() {
    /*Global $wpdb;
    $uip = $_SERVER['REMOTE_ADDR'];
    $login_info = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'bbr_login WHERE ip LIKE "'.$uip.'"', ARRAY_A);
    if(!is_null($login_info)) {
    $user_info = $wpdb->get_row('SELECT * FROM '.$wpdb->prefix.'bbr_usrs WHERE id = '.$login_info['id'], ARRAY_A);
    $wpdb->delete($wpdb->prefix.'bbr_login',array('id'=>$login_info['id']));*/
    global $user_info;
    setcookie('account_id',$user_info['id'],time()+3600,'/','www.register.dynamicpathwaysinc.com');
    //header('refresh: 4; url=/account');
    }

    I made a conversion from wordpress php to pure php … setcookie creates the right header to send and then the browser will be free to accept or not this command, the next time the browser sends the cookie does not generate warring while if it does it is because it does not exist with the isset check function when it exists so you don’t get an error.
    Only if the url starts and ends exactly like this https://www.register.dynamicpathwaysinc.com/

    Thread Starter brad3260

    (@brad3260)

    Thank you everyone for the suggestions, I’m still unable to get it to work so I’m thinking I am going to try javascript. I know I would need a js function that looks something like the following

    function setcookie(accid) {
    //code
    }

    However, how would I get the values from my PHP into the function call?

    you cannot directly call the cookie because initially it does not exist you have tried as suggested by me and that your function can also return false?

    https://www.ads-software.com/support/topic/n-set-cookie-wont-work/#post-11589141

    Thread Starter brad3260

    (@brad3260)

    Well, turns out my code was working, I had an SQL issue. Sorry about that but thank you everybody for the assistance!

    Moderator bcworkz

    (@bcworkz)

    No worries, I’m glad you resolved it.

    Yes those weren’t working, although i have started using the cookie consent for the cookie notice for gdpr

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘set cookie won’t work’ is closed to new replies.