Client Side Data saving with PHP/Javascript
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
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:
- user hits welcome page
- 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
- 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>
<?
}
}
?>
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
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.
精彩评论