Beppis Posted August 11, 2010 Share Posted August 11, 2010 Hi! I want to have a first page on my website where you as visitor can choose the preferred language. No login at all. I want to save this choice in a cookie or something. The easiest way So next time he/she visits, the language choice is already made for he/she. Any good suggestions for a php code that works? Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/ Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098046 Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 You could have it so on the index page it checks if the cookie exists and if not it goes to language.php Something like this: index.php <?php if(!isset($_COOKIE['language'])) { header("Location: language.php"); } else { $language = $_COOKIE['language']; ?> <html> <head> <title>Main Page</title> </head> <body> <h1>Main Page</h1> <?php echo 'Selected language: '.$language; ?> </body> </html> <?php } ?> language.php <?php if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <html> <head> <title>Site Name - Language Selection</title> </head> <body> <h1>Language Selection</h1> <form action="language.php" method="post"> <select name="language" id="language"> <option value="english" selected="selected">English</option> <option value="german">German</option> <option value="french">French</option> <option value="spanish">Spanish</option> <option value="andmore">And more...</option> </select> <input type="submit" name="submit" value="Submit"> </form> </body> </html> <?php } else { $language = $_POST['language']; setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds header("Location: index.php"); } } else { header("Location: index.php"); } ?> You would need to change this code to make the index page do something with the language to display that language. Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098053 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Nice But what if I want to send them to two different pages? if the language is swedish, locate = sv/index.php if the language is english, locate = index.php The website looks like this http://www.clickbuxer.com/language.php Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098074 Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 Does your website already have all the language files setup? And different code to make it display a different page per language: language.php <?php if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <html> <head> <title>Site Name - Language Selection</title> </head> <body> <h1>Language Selection</h1> <form action="language.php" method="post"> <select name="language" id="language"> <option value="english" selected="selected">English</option> <option value="german">German</option> <option value="french">French</option> <option value="spanish">Spanish</option> <option value="andmore">And more...</option> </select> <input type="submit" name="submit" value="Submit"> </form> </body> </html> <?php } else { $language = $_POST['language']; setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds switch($language) { case english: $redirect = "index.php"; break; case german: $redirect = "german/index.php"; break; case french: $redirect = "french/index.php"; break; case spanish: $redirect = "spanish/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } } else { $language = $_COOKIE['language']; switch($language) { case english: $redirect = "index.php"; break; case german: $redirect = "german/index.php"; break; case french: $redirect = "french/index.php"; break; case spanish: $redirect = "spanish/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098078 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks again! But what if I want the visitor to click on the images to choose language and not use that form that you showed me. How do I code an image to send that "submit" information? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098082 Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 You could have more than 1 submit/image button with different names and have each one have an image of a flag on it, something like this: <form action="language.php" method="post"> <input type="image" src="images/flags/english.png" width="16px" height="11px" border="0" name="submitenglish" alt="English"> <input type="image" src="images/flags/german.png" width="16px" height="11px" border="0" name="submitgerman" alt="German"> <input type="image" src="images/flags/swedish.png" width="16px" height="11px" border="0" name="submitswedish" alt="Swedish"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098092 Share on other sites More sharing options...
PradeepKr Posted August 11, 2010 Share Posted August 11, 2010 How about using htaccess file. Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098103 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Parse error: syntax error, unexpected $end in Line 106 if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><? include("title.php"); ?></title> <style type="text/css"> <!-- body { background-image: url(images/background.jpg); } --> </style></head> <body> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p><br /> <br /> </p> <div align="center"> <h1>Language Selection</h1> <table width="200" border="0"> <tr> <form action="language.php" method="post"> <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="submitswedish" alt="Swedish"></div></td> <td width="1"> </td> <td><div align="left"></div></td> <td><input type="image" src="images/usa.jpg" border="0" name="submitusa" alt="Usa"></td> </form> </tr> <tr> <td><div align="center"><strong>SWE</strong></div></td> <td> </td> <td width="0"><div align="center"></div></td> <td width="110"><div align="center"><strong>USA</strong></div></td> </tr> </table> </div> <p align="center"> </p> </body> </html> <?php if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <?php } else { $language = $_POST['language']; setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds switch($language) { case usa: $redirect = "index.php"; break; case swedish: $redirect = "sv/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } } else { $language = $_COOKIE['language']; switch($language) { case usa: $redirect = "index.php"; break; case swedish: $redirect = "sv/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } LINE 106!! Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098158 Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 Try: <?php if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><? include("title.php"); ?></title> <style type="text/css"> <!-- body { background-image: url(images/background.jpg); } --> </style></head> <body> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p><br /> <br /> </p> <div align="center"> <h1>Language Selection</h1> <table width="200" border="0"> <tr> <form action="language.php" method="post"> <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="submitswedish" alt="Swedish"></div></td> <td width="1"> </td> <td><div align="left"></div></td> <td><input type="image" src="images/usa.jpg" border="0" name="submitusa" alt="Usa"></td> </form> </tr> <tr> <td><div align="center"><strong>SWE</strong></div></td> <td> </td> <td width="0"><div align="center"></div></td> <td width="110"><div align="center"><strong>USA</strong></div></td> </tr> </table> </div> <p align="center"> </p> </body> </html> <?php if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <?php } else { $language = $_POST['language']; setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds switch($language) { case usa: $redirect = "index.php"; break; case swedish: $redirect = "sv/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } } else { $language = $_COOKIE['language']; switch($language) { case usa: $redirect = "index.php"; break; case swedish: $redirect = "sv/index.php"; break; default: $redirect = "index.php"; } header("Location: $redirect"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098160 Share on other sites More sharing options...
Adam Posted August 11, 2010 Share Posted August 11, 2010 Why not just pass the language in the URL? e.g. "example.com/index.php?lang=en" Then in the code you just need to check if the lang param has been passed, else use the cookie, else use the default: if (isset($_GET['lang'])) { // return lang (convert it to entities // to prevent any XSS attacks) $lang = htmlspecialchars($_GET['lang']); // store for next time setcookie('lang', htmlspecialchars($_GET['lang'])); } elseif (isset($_COOKIE['lang'])) { // set lang var $lang = $_COOKIE['lang']; } if (!isset($lang)) { // if no $lang var set, use default $lang = 'en'; } Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098167 Share on other sites More sharing options...
jackw899 Posted August 11, 2010 Share Posted August 11, 2010 Why not just pass the language in the URL? e.g. "example.com/index.php?lang=en" Yes this is probably the best way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098170 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks again But nothing happens when I click on the images It's not directing me to a page... Here's the code: <? if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><? include("title.php"); ?></title> <style type="text/css"> <!-- body { background-image: url(images/background.jpg); } --> </style></head> <body> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p><br> <br> <br /> <br /> </p> <div align="center"> <table width="200" border="0"> <tr> <form action="language.php" method="post"> <td width="114"><div align="left"><input type="image" src="images/swe.jpg" border="0" name="swedish" alt="swedish"></div></td> <td width="1"> </td> <td><div align="left"></div></td> <td><input type="image" src="images/usa.jpg" border="0" name="usa" alt="ssa"></td> </form> </tr> <tr> <td><div align="center"><strong>SWE</strong></div></td> <td> </td> <td width="0"><div align="center"></div></td> <td width="110"><div align="center"><strong>USA</strong></div></td> </tr> </table> </div> <p align="center"> </p> </body> </html> <?php if (isset($_GET['language'])) { // return lang (convert it to entities // to prevent any XSS attacks) $lang = htmlspecialchars($_GET['language']); // store for next time setcookie('language', htmlspecialchars($_GET['language'])); } elseif (isset($_COOKIE['language'])) { // set lang var $lang = $_COOKIE['language']; } if (!isset($lang)) { // if no $lang var set, use default $lang = 'en'; } if(!isset($_COOKIE['language'])) { if(!isset($_POST['submit'])) { ?> <?php } else { $language = $_POST['language']; setcookie("language", $language, time()+3600);;//set to expire in 1 hour, 3600=seconds switch($language) { case usa: $redirect = "http://www.clickbuxer.com"; break; case swedish: $redirect = "http://www.clickbuxer.com/sv/index.php"; break; default: $redirect = "http://www.clickbuxer.com"; } header("Location: $redirect"); } } else { $language = $_COOKIE['language']; switch($language) { case usa: $redirect = "http://www.clickbuxer.com"; break; case swedish: $redirect = "http://www.clickbuxer.com/sv/index.php"; break; default: $redirect = "http://www.clickbuxer.com"; } header("Location: $redirect"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098255 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 What could be the problem here? Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098265 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098271 Share on other sites More sharing options...
Beppis Posted August 11, 2010 Author Share Posted August 11, 2010 Hello? :'( Quote Link to comment https://forums.phpfreaks.com/topic/210435-language-selection/#findComment-1098277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.