www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP Cookie Issue on Redirect

  1. #1
    Join Date
    Dec 2013
    Posts
    12

    PHP Cookie Issue on Redirect

    I'm not exactly sure what the problem is but the cookies aren't available for subdomains. Is there any way around this, or is there a better way to set the cookies domain? Thanks!

    Code:
    <?php 
    ini_set("session.cookie_domain", ".example.com"); 
    
    // Set cookie and redirect when user change city 
    if( isset($_POST['city']) && $_POST['city'] != '' ){ 
        $cookie_expire = time() + 50400;  
        setcookie('city', $_POST['city'], $cookie_expire, '/'); 
    
        header("Location: http://".$_POST["city"].".example.com"); 
        die(); 
    } 
    
    // Redirect if user selected default city 
    if (isset($_COOKIE["city"])) { 
        $subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST'])); 
    
        if ($_COOKIE["city"] != $subdomain) { 
            header("Location: http://".$_COOKIE["city"].".example.com"); 
            die(); 
        } 
    }

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    PHP Code:
    // note leading "." in cookie domain
    setcookie('city'$_POST['city'], $cookie_expire'/''.example.com'); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2013
    Posts
    12
    Thanks NogDog but not working. I think the place that I add the code is wrong. I have added the same code to 2 index.php pages which belong to main domain and sub-domain. Both domains are running the same program with the same script Is it correct? Can you provide instruction?

    <?php
    ini_set("session.cookie_domain", ".example.com");

    // Set cookie and redirect when user change city
    if( isset($_POST['city']) && $_POST['city'] != '' ){
    $cookie_expire = time() + 50400;
    setcookie('city', $_POST['city'], $cookie_expire, '/', '.example.com');


    header("Location: http://".$_POST["city"].".example.com");
    die();
    }

    // Redirect if user selected default city
    if (isset($_COOKIE["city"])) {
    $subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));

    if ($_COOKIE["city"] != $subdomain) {
    header("Location: http://".$_COOKIE["city"].".example.com");
    die();
    }
    }
    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Have you checked in the browser to see if you are actually receiving the cookie? (I'm not sure what the expected behavior is on sending/saving a cookie when you also have a redirect header -- or if it varies by browser.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Dec 2013
    Posts
    12
    I have tested the web browsers (IE, Firefox and Chrome) by using some testing script and the cookie was working.

  6. #6
    Join Date
    Dec 2013
    Posts
    2
    On the first visit, user is cookied to index1 page. If I then remove index1 from the redirect and the same user returns to the index page, they get a blank page (in Safari browser).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles