Support » Plugin: Theme Switcha » Cookie creation fails for localhost:XXXX hosts

  • Resolved the8thcelljr

    (@the8thcelljr)


    When working from a localhost:XXXX (eg. localhost:9000) domain, the port number is not stripped when parsing HTTP_HOST, causing setcookie to fail. This renders theme switching completely unusable.

    The issue is in inc/plugin-core.php on line 43:
    $domain = sanitize_text_field($_SERVER['HTTP_HOST']);

    Using parse_url fixes the issue:
    $domain = parse_url(sanitize_text_field($_SERVER['HTTP_HOST']), PHP_URL_HOST);

    Is it possible to submit a pull request for this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeff Starr

    (@specialk)

    It’s possible to suggest a solution or workaround. I don’t develop on localhost though, so it will have to be something solid if it’s going to be implemented. Let me know what you have in mind.

    Thread Starter the8thcelljr

    (@the8thcelljr)

    I think you could avoid any regression for current users with the following solution:

    By default, use the current code to parse the host:
    $domain = sanitize_text_field($_SERVER['HTTP_HOST']);

    Then, check for the existence of a port number in the URL with parse_url:
    $port = parse_url($domain, PHP_URL_PORT);

    If a port number exists, assume the domain is a local URL and parse the host name with parse_url:

    if (!empty($port)) {
        $domain = parse_url($domain, PHP_URL_HOST);
    }
    

    What do you think?

    • This reply was modified 1 year, 5 months ago by the8thcelljr.
    • This reply was modified 1 year, 5 months ago by the8thcelljr.
    Plugin Author Jeff Starr

    (@specialk)

    Yes the logic looks solid. I will take a closer look and try to implement for the next plugin update. Thank you for your help @the8thcelljr.

    Thread Starter the8thcelljr

    (@the8thcelljr)

    Thanks! I appreciate it!

    Plugin Author Jeff Starr

    (@specialk)

    I have implemented this in the next version (2.8) of Theme Switcha. Should be available later today. Please test the cookies on localhost and let me know the results. Thank you, @the8thcelljr.

    Thread Starter the8thcelljr

    (@the8thcelljr)

    @specialk
    Hey, thanks for the follow up!

    I just checked and it works fine now. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Cookie creation fails for localhost:XXXX hosts’ is closed to new replies.