Jump to content

php set cookie redirect


Recommended Posts

I need a domain example.com redirects to sub-domain a.example.com when I type on the address bar.

<script type="text/javascript">
    var city = readCookie('city');
    if(city==null && city==''){
    window.location.href = 'http://' + city + '.example.com';
    var city = $(this).val();
    createCookie('city', city, 28);
    window.location.href = 'http://' + city + '.example.com';

<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>

The cookie on the server side is not holding so the domain cannot remember sub-domain. What am i doing wrong? Any help will be very much appreciated.

$hour = time() + 50400;  //Time you want the cookie to last, currently 14 hours 
setcookie(My_Site_Location, $_SERVER['citygo'], $hour, '/', 'example.com');

if (isset($_SERVER['citygo'])) {
   $cookies = explode(';', $_SERVER['citygo']);
   foreach ($cookies as $cookie) {
       list($cookie_id, $cookie_value) = explode('=', $cookie);
       if($cookie_id === $name){
          self::set_cookie($cookie_id, $value, $expiry, $path, $domain);
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.