php set subdomain cookies and redirect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gmax
    New Member
    • Dec 2013
    • 3

    php set subdomain cookies and redirect

    I have the cookies and subdomain selection for header:

    Code:
    <script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(function(){
    var city = readCookie('city');
    if(city !=null && city !=''){
    window.location.href = 'http://' + city + '.example.com';
    }
    $('#citygo').change(function(){
    var city = $(this).val();
    window.location.href = 'http://' + city + '.example.com';
    });
    });
    function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }
    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }
    function eraseCookie(name) {
    createCookie(name,"",-1);
    }
    </script>
    <select id="citygo">
    <option value="0">Select City</option>
    <option value="amsterdam">Amsterdam</option>
    <option value="newyork">New York</option>
    <option value="london">London</option>
    <option value="cardiff">Cardiff</option>
    </select>
    Now I need to work on the server side to set cookies to remember and redirect to a visited subdomain. The code below is not working but should be something like that. Would someone show me how to set cookie? Any help will be very much appreciated.

    Code:
    <?php 
    if (isset($_COOKIE["city"])) { 
    if ($_COOKIE["city"] == 'city') { 
    header("window.location.href = 'http://' + city + '.example.com'"); 
    } 
    } 
    ?>
Working...