duanmeng7865 2013-09-27 06:51
浏览 44
已采纳

多语言网站:php + mysql + cookies

I have coded a multilingual web with php & mysql. Now I've faced problems (mainly with robots, seo and search engines) because language chooser is based on cookies.

how it works now: user => index.php :

function cookies() {
 if (!isset($_COOKIE["lang"])){
 setcookie('lang','ukr', time()+(60*60*24*31)); 
 header('Location: index.php');  
}};

and outputting chosen language to every place it needed to be with the function:

function catSet($source, $link, $item){
 $item .= '_'. $_COOKIE['lang'];
 global $connection;
 $link = mysqli_real_escape_string($connection, $link);
 $result = mysqli_query($connection, "SELECT * FROM  $source WHERE link_item = '". $link ."'");
if (!$result) { die("DB Query failed: " . mysql_error()); }
while ($row = mysqli_fetch_array ($result)) {
 echo $row[$item]; }}

What I want now is to have $item .= '_'. $_COOKIE['lang']; to be replaced with constant based on a user choice which would be readable by robots and search engines, so it's a must it's not a cookie.

I don't want to use session, because this way user have to re-choose language each time after session expires.

AFAIK there is no way to detect browsers language, to set it as default.

I see a way out to use [GET] variable instead, and pass it each time to other page, but how to store this [GET] data if user come back next time (same problem as with sessions).

I would appreciate any ideas and solutions :)

  • 写回答

2条回答 默认 最新

  • dongliaoqiang8524 2013-09-27 06:54
    关注

    You can still use cookies to save user decision. When user visit your website without language in url (GET) but got cookie with selected language, redirect him to url with selection.

    It'll allow you to use addresses with given language and also will allow user to just copy/past his current URL which will always lead him to his language version.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?