views:

67

answers:

3

Dear Programmers,

I want to make a website that would be like craiglist.org. If you go the site and click on any of the city it will redirects you to the city subdomain. And after that anytime you go to the craiglist.org it will redirect you to the city subdomain. So I need to keep the city/country name to my client's machine.

At first I thought I can do it by Cookie with setting expire time. I tried that but the expire time is not working as when i close the browser that means when session ends it is not getting the cookie anymore.

But if it was ok with cookie then also it may not be ok for the destination as if I browse the site from Mozilla and closed and open the site with IE it should redirects me to the visited site that is not possible with cookie i think?

Can you please give me some suggestion ? How can I do the thing. I am going to do the application with Codeigniter/Php.

Thanks Sumon

+1  A: 

The cookie is the best way to do this; if the web browser's cookie is not being reloaded after closing the browser, you must have something wrong when creating the cookie.

Have a look at the PHP docs for setcookie(): http://us3.php.net/set_cookie, make sure you are providing an expire time far in advance, such as strtotime("+300 days"). On the server, to read the cookie back, simply use the $_COOKIE super global. Link: http://us3.php.net/manual/en/reserved.variables.cookies.php

Also, be sure to check your browser settings for the handling of cookies... maybe it is set to delete cookies after closing the browser.

Finally, you could also use the user's IP address -- but be warned that users can share the same IP address if they are on the same local area network, such as a university or corporation. You could even auto-choose the location by using an IP location lookup service such as http://ipinfodb.com/


You can inspect your website's cookies in Firefox by going to

Tools -> Page Info -> Security -> View Cookies

Michael Butler
I used correct way to setcookie with setting time but it was not working need to see the browser setting. But if that works it will not solve the problem as if i browse with IE and again browse with Mozilla it will not get the cookie created by IE?
Sumon
Correct, each browser has their own set of cookies.
Michael Butler
I think with .swf we can make the cookie for all broswer ? But is there another way by using only javascript or like this?
Sumon
A: 

It seems illogical to try to save browser state across browsers. If a user switch browser, they are usually not guaranteed to have the same browser settings, unless they have an account on that site.

If they are registrered users, you should store whatever state you need in your database, and retrieve it on login. Then you can always cache the data in a client-side cookie, so you don't need to fetch it everytime.

In my opinion, storing browser events across different browsers for anonymous users would not be worth the efford.

Mads Jensen
+2  A: 

cookie is the right approach. you cant achieve something cross-browser ... well you could use flash cookies or re detect the ip adress etc but thats stupid...

well craiglist.org seems to redirect me according to where my ip adress comes from. to do this you should check out this site: http://www.maxmind.com/app/geolitecity

generally speaking you should always give the user the possibility to switch by hand.

from a usability optimizing point of view i would suggest the following scenario:

  1. user hits welcome page
  2. welcome page detects city of the user using geoip 3.1 if city has been detected successfully user gets bounced directly to the subsite, cookie is saved 3.2 if city is not successfully detected, welcome page shows a selection list. once the user clicks a selection list the cookie is set and the user gets the specified subsite
  3. on every subsite there is a well visible button on the top "switch city" or something. if the user clicks it he gets to the welcome page, but with a parameter "userelection=true" ro something, so that the automatic ip detection and redirection doesng kick in. the user selects the desired subsite, gets a cookie and gets redirected. 5. if the user hits the welcome page again and a cookie is found he is redirected to the subsite, cookie can be overriden, thats no problem...

the easiest and bset solution would be setting the cookie using php and then do a header redirect!

this way you eliminate javascript, which can be turned off or something.

just use this php command:

setcookie("city", $value, time()+(86400*365));

or something similar. heres the manpage:

http://php.net/manual/en/function.setcookie.php

you can check for cookie values using

if($_COOKIE['city'] != '') 

edit:

well as of the confusion i have an working example for you here now, just paste it into a php file and save it to your webserver :) the check for subsite won't be necessary as you said you are redirecting to external sites (subdomains) anways. keep in mind that you cannot access a cookie from a subdomain if you set it on another subdomain! maby this was your problem, therefore the checking and redirecting int he bootstrap file.

well here you go. any questions? comment!

    <?
/* this is if we are at a subsite. you wont need that as it will be external subdomains anway just for the purpose of a working example here.*/
    if($_GET['subsite']!="") {
/* the change link has the paramenter userselection which overrides any autoamtic detection by cookie or ip or whatever, this is important if the user wants to change or the automatic change fails or is wrong*/
     ?>
      you are now at the subsite of <?=$_GET['subsite'];?><br/>
      <a href="?userselection=true">click here to change</a>
      <?
    }
    else {
/* only try to automatically redirect if we have either cookie or user specified data, but dont if the flag userselectino is true. because the user wants to change.
you could ip-detect the city at this point.
*/
     if(($_GET['go']!=''||$_COOKIE['city']!='')&&$_GET['userselection']!='true') {
/* if we had ip detection the priority of it would probably go here */
      $target=$_COOKIE['city'];
/* the get varaible overrides the cookie value as its user supplied and more important*/

      if($_GET['go']!='') {
       $target=$_GET['go'];
      }
/* set the cookie, don't care if it already has been set*/
      setcookie('city',$target,time()+(86400*365));
/* redirect the user */
      header('Location: ?subsite='.$target);
     }
     else {
/* well we either have neither cookie nor user specified city or the user wanted to change teh city, lets display the selection dialog!*/
      ?>
        hello! select your city:<br/>
        <a href="?go=vienna">vienna</a>
        <a href="?go=newyork">new york</a>
        <?
     }
    }
    ?>
Joe Hopfgartner
But I used cookie with the same way you said likesetcookie('city', 'boston', time()+60*60*24*6004);But it is not working after browser close. And also it is not ok for cross browser?
Sumon
well then you got something wrong. ill provide you with some working code shortly, will add it to the answer as edit
Joe Hopfgartner
Joe, Is there any way to find the saved cookie from other browser? I think not?
Sumon
I think with .swf ? But is there another way by using only javascript or like this?
Sumon
yes with swf its possible. but please dont do that. usual cookie is just fine! and swf doesnt guarantee as well, for example firefox and chrome have their own plugin and internet explorer uses the flash player installed on the system... so this wont work either ;)javascript: no
Joe Hopfgartner
your example is ready with comments and everything.
Joe Hopfgartner
hi many thanks for the example you wrote. I appreciate the help. Actually I did the same thing which you gave example but it was not working i think because of the framework i am using (Codeigniter). Now it is working .... why working i don't know :). Anyway I got a tutorial about the cross site cookie with flash .. http://www.nuff-respec.com/technology/cross-browser-cookies-with-flash should i not use that?
Sumon