[resolved] Problem with cookies and multisite (6 posts)

  1. jbrule
    Posted 1 year ago #

    The rated check does not work correctly when deployed to a directory based multisite install.

    FYI My multisite install is deployed to /charlie/ and the subsites live at /charlie/****/

    For example the site with path /charlie/test-josh/

    Is setting the cookie as so
    Set-Cookie rated_6138=1; expires=Sat, 15-Nov-2014 02:16:34 GMT; path=/charlie/

    The problem with this is that it will act as if the voter has already voted for the post with an id of 6138 on all subsites that are deployed under /charlie/ .

    It appears that as implemented the cookie + IP option does not negate with problem either as the cookie is checked first and then the IP is checked only if the cookie check returns false.

    To resolve the issue I suggest setting the cookie path to the path with is returned by site_url();

    The following is the problem code from 1.65. The COOKIEPATH below returns the root path of the multisite install.
    $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, COOKIEPATH);


  2. Lester Chan
    Plugin Author

    Posted 1 year ago #

  3. jbrule
    Posted 1 year ago #

    Thanks for the quick turnaround. Unfortunately the change is not working for my install. The SITECOOKIEPATH global is still just returning /charlie/

    I did have success doing the following.

    I added a filter call to the plugin to allow me to override and resolve the cookie path in my functions.php file

    The following change was made to line 622 in the wp-postratings.php file
    $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, apply_filters('wp_postratings_cookiepath',SITECOOKIEPATH));

    Then in my theme functions.php file I added

    function wp_postratings_set_cookie_path($the_cookie_path){
    return preg_replace( '|https?://[^/]+|i', '', get_option( 'siteurl' ) . '/' );
  4. Lester Chan
    Plugin Author

    Posted 1 year ago #

    @jbrule i checked the WP core code, it seems that SITECOOKIEPATH is used for most of the cookies set by WP itself

  5. jbrule
    Posted 1 year ago #

    Yes it is but it appears all of the those cookies have their paths set in relation to the SITE (in wp_sites) as opposed to the BLOG (in wp_blogs). The use of the cookie in the plugin is still broken in this case.

    See wpmu_current_site() in http://core.svn.wordpress.org/tags/3.4.2/wp-includes/ms-load.php and you can see how the path is being set to the value from wp_site. ms_cookie_constants( ) uses that value for the SITECOOKIEPATH in a multisite install. The SITECOOKIEPATH is not the siteurl (which it needs to be for this plugin to work correctly).

    I see three options to make this work correctly

    1. Set the cookie path value using preg_replace( '|https?://[^/]+|i', '', get_option( 'siteurl' )
    2. Add the blog_id to the cookies key so you get rated_12_6138 instead of rated_6138 (less desirable as you are unnecessarily passing around cookies in a subdirectory install.)
    3. Implement a filter to allow overriding the path for those installs that do not work with the SITECOOKIEPATH. (probably wouldn't hurt implementing this regardless).

  6. Lester Chan
    Plugin Author

    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic