Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts

    cookie not working without the WWW

    Not sure what i am doing wrong...

    Here is the page used to set the cookies: https://www.ni-dieu-ni-maitre.com/test.php

    $domain = "ni-dieu-ni-maitre.com";
    $articleid = "test";

    $lastviewedarticles = array();

    if (isset($_COOKIE["viewed_articles"]) ) {
    $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
    }

    if (!in_array($articleid, $lastviewedarticles)){
    $count = count($lastviewedarticles);
    if($count>=29)
    array_shift($lastviewedarticles);
    $lastviewedarticles[] = $articleid;
    }
    setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', '.' . $domain);
    Then this page reads the cookie and output the content: https://ni-dieu-ni-maitre.com/test2.php
    if ( isset($_COOKIE["viewed_articles"]) ) {
    $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
    }
    echo "cookie is currently:<br>";
    print_r($lastviewedarticles);

    After setting the cookie with test1.php, the cookie can be read with WWW but is empty without WWW

    https://ni-dieu-ni-maitre.com/test2.php = empty cookie
    https://www.ni-dieu-ni-maitre.com/test2.php = working

  • #2
    SSJ
    SSJ is offline
    Regular Coder
    Join Date
    Mar 2007
    Posts
    230
    Thanks
    0
    Thanked 4 Times in 4 Posts
    There is nothing wrong with your PHP code. This is a browser thing. They treat both non-www and www domains as seperate and store cookie accordingly. When setting the cookie, use .mydomain.com (with the leading dot). This will tell your user's browser make the cookie accessible to mydomain.com and all subdomains, including www. PHP's setcookie has the argument $domain, but it's fifth on the list, so you may need to set $expire and $path to their default values in order to get at it.

    But as a friendly suggestion I will suggest you to implement canonical URL redirect for your domain so you won't have this issue anymore.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SSJ View Post
    There is nothing wrong with your PHP code. This is a browser thing. They treat both non-www and www domains as seperate and store cookie accordingly. When setting the cookie, use .mydomain.com (with the leading dot). This will tell your user's browser make the cookie accessible to mydomain.com and all subdomains, including www. PHP's setcookie has the argument $domain, but it's fifth on the list, so you may need to set $expire and $path to their default values in order to get at it.

    But as a friendly suggestion I will suggest you to implement canonical URL redirect for your domain so you won't have this issue anymore.
    it already has a dot before $domain

    '.' . $domain);
    but looks like it will work if i put the dot right inside $domain variable:
    $domain = ".ni-dieu-ni-maitre.com";
    setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', $domain);
    Now my cookie isn't empty anymore, but if i echo the unserialized cookie, i get this:
    Array ( [0] => test ) array(1) { [0]=> string(4) "test" } test
    don't understand what's wrong... why do i get another array inside my array ?

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    0 down vote


    I believe you can set the cookie at example.com (really .example.com) and it will be sent if they go to www.example.com, but not vice versa. This standard security policy is to prevent users' private data from being sent to unintended servers.

    Personally, I use virtualhosts in my apache2.conf:

    <VirtualHost *:80>
    ServerName example.com
    RedirectMatch (.*) http://www.example.com$1
    </VirtualHost>

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    But it doesn't explain why i have an array inside another array in my cookie.... i'm confused

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by anarchoi View Post
    But it doesn't explain why i have an array inside another array in my cookie.... i'm confused
    You're not the only one! This is the first you've mentioned of it. The issue you posted was about a cookie not being read and now you're talking about something related but different

    I've tested your pages and can confirm that the cookie is not sent to the script without the www. I'd go with chris's reply on this as its clearly intentional behaviour as I used a custom written program using Indy sockets to test your code. Indy strictly follow RFC standards so your cookie is not submitted due to some RFC standard. In essence, what is happening is the correct behaviour. I know that doesn't help you but at least you know its not a bug - meaning you can find a workaround
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,454
    Thanks
    0
    Thanked 632 Times in 622 Posts
    If you want a cookie to apply across the whole domain whether the www or anything else is on the front or not then specify a value for the domain parameter in the cookie.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I don't know how to edit my apache2.conf

    If you want a cookie to apply across the whole domain whether the www or anything else is on the front or not then specify a value for the domain parameter in the cookie.
    Already did it. Look at the code.

  • #9
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Found a workaround with htaccess:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


    Edit: doesn't work
    http://subdomain.mydomain.com will be redirected to http://www.subdomain.mydomain.com

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by felgall View Post
    If you want a cookie to apply across the whole domain whether the www or anything else is on the front or not then specify a value for the domain parameter in the cookie.
    Re-read the op felgall

    The source clearly shows the op is specifying a value for the domain.

    @anarchoi, did you try chris's virtual host idea?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #11
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    @anarchoi, did you try chris's virtual host idea?
    i have really no idea how to set up a virtual host

  • #12
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I ended up finding a solution with htaccess that will not redirect the subdomain:

    RewriteEngine On
    # Matching any of 3 domains without www, and no subdomain
    RewriteCond %{HTTP_HOST} ^(ni-dieu-ni-maitre|no-gods-no-masters|ni-dios-ni-amo)\.com$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Thats because HTT_HOST is the domain OR subdomain - what ever the browser requested in the request headers. If it was a subdomain then it will become www.subdomain.domain.com

    As for virtual hosts, they're setup in apaches config file but I think you can also put them in the .htaccess file of your main folder. GOOGLE is your friend here.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •