1

I have a website that has multiple locations and each location has their own set of information. When a user visits the main corp page, the location information will not display. When the user then goes to a location page I have a cookie being set.

        $location_address = get_field('location_address','options');
        $location_city = get_field('location_city','options');
        $location_state = get_field('location_state','options');
        $location_zip_code = get_field('location_zip_code','options');
        $location_phone_number = get_field('location_phone_number','options');

        // cookie will expire when the browser close
        setcookie("locationAddress",$location_address);
        setcookie("locationCity",$location_city);
        setcookie("locationState",$location_state);
        setcookie("locationZipcode",$location_zip_code);
        setcookie("locationPhone",$location_phone_number);

The cookie is showing my location information in the firebug console. However, what is happening is when I go to another location, its grabbing both sets of cookies. I need it to replace the cookie previously set with the new one.

This is my code as well to output the cookie, which seems to be breaking as well:

<div class="top_contact">
                <p><?php if(!isset($_COOKIE[$location_address])) { echo "" . $location_address . "";} ?>, <?php if(!isset($_COOKIE[$location_city])) { echo "" . $location_city . "";} ?> <?php if(!isset($_COOKIE[$location_state])) { echo "" . $location_state . "";} ?> <?php if(!isset($_COOKIE[$location_zip_code])) { echo "" . $location_zip_code . "";} ?> | <span class="top_phone"><?php if(!isset($_COOKIE[$location_phone_number])) { echo "" . $location_phone_number . "";} ?></span> </p>

            </div>
  • Multiple locations? What you mean? Multiple sub-domains? Can you explain? – b4x Aug 24 '18 at 16:48
  • Yes I am using a wordpress mult-site for location pages. So I have a corporate main site and then multi-sites in subfolders. So for example, you go to domain.com and then domain.com/los-angeles. When you go back to domain.com the address and phone number will follow you. Then if I went to domain.com/las-vegas and then back to domain.com, the los-angeles information would be replaced with the las-vegas information. Make sense? – BrandonRob0422 Aug 24 '18 at 16:52
  • this seems like a duplicate of https://stackoverflow.com/questions/18492576/share-cookie-between-subdomain-and-domain – Ggg Aug 24 '18 at 16:58

2 Answers2

2

You should set "path" argument in setcookie.

setcookie("locationAddress",$location_address, 0, "/");
setcookie("locationCity",$location_city, 0, "/");
setcookie("locationState",$location_state, 0, "/");
setcookie("locationZipcode",$location_zip_code, 0, "/");
setcookie("locationPhone",$location_phone_number, 0, "/");
b4x
  • 81
  • 4
0

If I were you I would probably try and set the path for the cookies.

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

also, I spotted a few commas in your code and I fixed the spaghetti code.

 <?php 
    echo '<div class="top_contact"><p>';

    if(!isset($_COOKIE[$location_address])) 
       { 
          echo $location_address;
       } 
    if(!isset($_COOKIE[$location_city])) 
       { 
          echo $location_city;
       }
    if(!isset($_COOKIE[$location_state]))
       { 
          echo $location_state;
       } 
    if(!isset($_COOKIE[$location_zip_code])) 
       { 
          echo $location_zip_code;
       } 

    echo '<span class="top_phone">';

    if(!isset($_COOKIE[$location_phone_number]))
       { 
          echo $location_phone_number;
       }
    echo '</span> </p> </div>';
?>
  • Using this method doesnt output any values into my code. I see the p and the span but the values are blank – BrandonRob0422 Aug 24 '18 at 17:33
  • Also, because I am using that code on the main corporate page, the cookie that was set from a location page is immediately overwritten on the corporate page. – BrandonRob0422 Aug 24 '18 at 18:27