• Hi

    I’m trying to create a cookie from information retrieved from a MySQL database.

    Is there a simple way I can do this without getting the ‘header already sent’ error?

    I’ve tried various plugins, but these don’t seem to work. I’m not exactly a newbie but this seems to be outside my expertise.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Not sure what you’re collecting from the database and what kind of query you want to do to acheive that but I set a cookie the other day by putting some simple functions in my functions file. Basically when a user first comes to this site, a multi language site, it ask which language you want. Then, it sets a cookie so you don’t have to be asked again and redirects you to the languages homepage.

    This function set the cookie:

    function site_set_user_language() {
        if(!isset($_COOKIE['site_language'])){
            setcookie('site_language', 'en', time() + (86400 * 7));
        } elseif (is_page('esp')){
           		setcookie('site_language', 'esp', time() + (86400 * 7));
        } elseif (is_page('fr')){
                setcookie('site_language', 'fr', time() + (86400 * 7));
        }  elseif (is_page('en')){
                setcookie('site_language', 'en', time() + (86400 * 7));
        }
     }
    add_action( 'wp', 'site_set_user_language',10,1);

    Then this function checked the cookie and redirected:

    function site_lang_home_redirect()
    {
        if( is_front_page() ){
    
    		$value = $_COOKIE['site_language'];
    
    		if($value == 'esp') {
            wp_redirect( get_bloginfo('url') . '/esp');
            exit();
    		} elseif($value == 'fr') {
            wp_redirect( get_bloginfo('url') . '/fr');
            exit();
    		} elseif(is_page('en')) {
            wp_redirect( get_bloginfo('url'));
            exit();
    		}
        }
    }
    add_action( 'template_redirect', 'site_lang_home_redirect',9 );

    Thread Starter Mumsbis

    (@mumsbis)

    Hi

    Thank you so much for helping me.

    So far I’ve tried modifying your code, but no luck.

    Basically, I have an entry in the database that I used to create unique tables for each person. I want to put use that entry so that they can call back the tables and so want to put this entry into a cookie.

    But I’m sure I’ll get there ??

    Claire

    Moderator bcworkz

    (@bcworkz)

    Hi Claire,
    I take it you are still getting the headers already sent error? You cannot use setcookie() if anything at all has been sent back to the browser. The setcookie() call must occur early enough in the WP initialization to avoid this.

    If the nature of your app requires cookies be set after content is sent you could use javascript inside an inline <script> block to set the cookie.

    In subsequent requests, PHP’s $_COOKIE will still contain your cookie values despite being set by javascript.

    Thread Starter Mumsbis

    (@mumsbis)

    Hi

    I finally got to do it using Sessions which is a relief as javascript scares me lol ??

    But thank you for checking ??

    Claire

    Just want to point out that in general, creating a table for every user is not considered a good solution.

    Instead, create one table to be shared by all users, and add an “identifier” column to distinguish which values belong to which user. Store that identifier value in a $_SESSION variable.

    Regardless of method, you’ll also want to make sure the identifier is some form of unique hash such that any user can’t change their $_SESSION cookie value and access data created by another user. In particular, naming them 1,2,3… would be a security concern.

    wjh.

    Thread Starter Mumsbis

    (@mumsbis)

    Ah now that’s a much better idea – why didn’t I think of that?

    The cookie value is a random number generated when they register that then stays with them, so hopefully this should make it much more secure.

    Thanks again for your help, really appreciate it

    Claire

    A slightly more secure method would be to provide a key and a hash.

    1. key: a random number and/or string (example “hello34”)
    2. hash: a hash of this key with a secret salt. (example md5( $mysalt . "hello34" ) )

    Store and retrieve both of these values from the $_SESSION variable.

    Then, before providing any data back to the user, use the key to check that the users hash is valid.

    Thread Starter Mumsbis

    (@mumsbis)

    Hi Will

    Thank you – that’s worked a treat (I didn’t use hello34 by the way).

    And I’m changing to just one table and retrieving as suggested.

    Thanks again

    Claire

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Creating a cookie’ is closed to new replies.