Simple switch between lang php with cookies - php

First I would like to check which language the user is using at the moment (which I have done).
After that if the language isn't the same as the website, overlay with a message that should pop up with question
Do you want change to your language ? click here
The link should redirect to another domain.
If the user choose to be redirected to another domain let's say DE, cookies should keep that settings at least one month and always redirect that user to DE domain even if he enter the main domain.
Here is what I have for now:
<?php
$cookie_name = "user";
$cookie_value = "";
setcookie($cookie_name, $cookie_value, time() + (86400*36) , "/"); // 86400 = 1 day
?>
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "pl":
//echo "PAGE FR";
$domain = 'click here';
break;
case "en":
$domain = 'click here';
break;
default:
$domain = 'click here';
break;
}
HTML
<div class='overlay'>You are using ENG lang atm do you want switch to ?
<?php echo $domain; ?> </div>

First, you have to save the users selection
I would do this with a page between the destination and your current page. For example the link to your one other domain would be http://example.com/changelang.php?target=domain1
there you save the cookie with the desired language
if(isset($_GET['target']) AND $_GET['target'] == 'domain1')
{
setcookie($cookie_name, "domain1", time() + (86400*36) , "/"); // 86400 = 1 day
and the redirect to this page
header('Location: http://de.domain1.com/');
exit;
}
Second, you have to check if there is an existing cookie
<?php
if(isset($_COOKIE[$cookie_name]))
{
//do switch here
switch($_COOKIE[$cookie_name])
{
case 'de':
//redirect to other page
header('Location: http://de.domain1.com/');
exit;
break; //this is useless
}
}

Related

I can't set a php cookie

I made a php script that allows me to switch the language of my website and for that, I used a cookie called lang to store the user's language prefer but I don't Know why this cookie can't be set as I can't find it in the chrome developer tap .. Although I made this script also with session variable in case cookie isn't set, it doesn't work either, as it shifts the language whenever I change the page
this is my PHP script
<?php
session_start();
if(isset($_GET['lang'])) {
$lan = $_GET['lang'];
$_SESSION['lang'] = $lan;
setcookie("lang", $lan, time() + (3600 * 24 * 30), '/');
} else if (isset($_SESSION['lang'])) {
$lan = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
$lan = $_COOKIE['lang'];
} else {
$lan = 'en';
}
include "languages/" . $lan . ".php";
?>
I have two files in that language folder en.php and ar.php and each one contains an array with translation like below
<?php
$lang = array(
"Give" => "Give",
"Blood" => "Blood",
"Register as a donator" => "Register as a donator",
"Search for a donator" => "Search for a donator"
I include my script in all my site's pages
here is my chrome developer tap screen shot
any thoughts?
Thanks for helping in advance :)

How can I switch the language of my page and stay at the scrolled position of the page?

I want to switch the language of my page. This works very well:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang'])) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
} else {
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
default:
$lang_file = 'lang.de.php';
}
include_once 'languages/' . $lang_file;
?>
If I click on for example "English"...
English
German
...my English page version is loaded like this:
www.mypage.php?lang=en
My problem is: when I scroll down the page and then click on another language,the page loads at the top position. But I would like the page to stay at the scrolled position. (Actually when I just refresh the page, then my page keeps staying at the scrolled position.)
You can take English and German in a span with unique id like this
<span id="unique_id_for_english">English<span>
<span id="unique_id_for_german">German<span>
You will have to use
event.preventDefault();
On your a tag, this way it will stop the default reload which causes the page to refresh to the top.
for example
$('a').click(function () {
event.preventDefault();
});

"This webpage has a redirect loop" (redirect website with GeoIP)

I'm trying to add some code to my website, in order to redirect users depending on their IP address. Whatever I do I'm getting a "This webpage has a redirect loop" message.
Here's my code:
if(isset($_GET['FirstTimer'])){
setcookie('FirstTimer','something',strtotime('+1 year'),'/');
$_COOKIE['FirstTimer']='something';
}
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
if(!isset($_COOKIE['FirstTimer'])):
if($_SERVER['REQUEST_URI'] == '/'){
switch((string)$country) {
case 'GR':
header('Location: http://mywebsite.com/');
break;
case 'RU':
header('Location: http://mywebsite.com/ru');
break;
default:
header('Location: http://mywebsite.com/en');
}
}
endif;
geoip_close($gi);
The "FirstTimer" cookie is used to determine whether the visitor is a new one or not (because I'd like to redirect new visitors only).
The code is placed on the top of my index.php file, and there are no spaces before '\<\?php ?>' tags.
I am assuming that $_GET['FirstTimer'] is not set. So your switch is always executed.
When geoip returns GR your script will redirect to http://mywebsite.com/ again.
$_SERVER['REQUEST_URI'] returns / and you are back in your switch again.
There is your loop.
EDIT
I've changed your script a little bit. Instead of logging if the user visits the first time, which makes no sense, just log when the user visited the last time.
If this cookie is not set, start geoip and redirect the user. If it is set, update the time.
if(!isset($_COOKIE['last_visit'])) {
setcookie('last_visit',date("Y-m-d H:i:s"),strtotime('+1 year'),'/');
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
if($_SERVER['REQUEST_URI'] == '/'){
switch((string)$country) {
case 'GR':
header('Location: http://mywebsite.com/');
break;
case 'RU':
header('Location: http://mywebsite.com/ru');
break;
default:
header('Location: http://mywebsite.com/en');
}
}
geoip_close($gi);
} else {
setcookie('last_visit',date("Y-m-d H:i:s"),strtotime('+1 year'),'/');
}
But you should definitely enhance this script because what happens, if you have a user that gets on your page with this: http://yourwebsite.com/great/article/link.html.
Perhaps you do have a translation for this, but the user won't be redirected.

Language based on HTTP_ACCEPT_LANGUAGE and database languages

I have a web site made in 3 languages, but will (maybe) be expanded.
So I created a table on the database where I insert the languages of the Website.
As index.php I have a script. If the URI detected is "/" the script redirects to /language/index.php.
I'd like now to have a script which dinamically redirects based on HTTP_ACCEPT_LANGUAGE.
My problem is that, the following script I made works only with the first foreach run.
$abbr = $link->query("SELECT abbr AS langs FROM LANGS");
while($langs_db = mysqli_fetch_array($abbr)){
$site_langs[] = $langs_db['langs'];
}
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
foreach($site_langs as $lang){
switch($client_lang){
case $client_lang == $lang:
header("location: http://www.mysite.com/$lang/index.php");
break;
default:
header("location: http://www.mysite.com/$lang/index.php");
break;
}
}
I thought a solution like:
case $client_lang == $lang:
$find "location: http://www.mysite.com/$lang/index.php";
break;
default:
$not_find = "location: http://www.mysite.com/$lang/index.php";
break;
And then:
if($find != ''){
header($find);
}else{
header(not_find);
}
But I'm not sure this it's a good idea...
Thank you!
// Set the default language as a fall back
$default_lang='en';
// Have an array with all languages your site supports
$langs=array('en', 'es', 'pt', /* etc... */);
// Query the browser's first preferred language
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// See if the language is supported otherwise redirect use the default one
$client_lang = (in_array($client_lang, $langs) ? $client_lang : $default_lang);
header("Location: /$client_lang/index.php");
Alternatively though you may want to consider gettext:
http://php.net/manual/en/book.gettext.php

How to set php cookie for a redirect

When people visit my site, I need them to click one of two states (NC or VA). Depending on which state is clicked, it will redirect them to the appropriate page on my site. After the cookie is set, I want them to visit the site and instead of asking the question again, it already knows to send them to the page (the state) they selected. I know very little about php...just enough to be dangerous and any direction you can give me would be appreciated.
You can set the cookie as:
setcookie('state', $state, time() + (60 * 60 *24));
Assuming $state is either 'nc' or 'va', this will work:
if(isset($_COOKIE['state']))
{
if($_COOKIE['state'] == 'va')
header('Location: va/index.php');
else if($_COOKIE['state'] == 'nc')
header('Location: vnc/index.php');
}
else
{
// Make them choose again here.
}
You should take a look at this php function setcookie.
setcookie( "state", "VA", time()+3600 );
Then redirect using the location header.
header( "Location: /" );
On the index page
<?php
if(isset($_COOKIE['state']))
{
switch($_COOKIE['state'])
{
case "NC":
header('location: www.url.com/site1/');
break;
case "VA":
header('location: www.url.com/site2/');
break;
}
}
else
{
//Display site options
}
?>
And on the individual sites (EG www.url.com/site1/):
<?php
if(!isset($_COOKIE['state']))
{
setcookie('state', "NC" ,time() + (86400 * 7)); //valid for 7 days
}
?>

Categories