I've been google-ing this for a while, and I have reviewed my code over and over again, still no clue about it.
Here is the deal:
I have a multi-language website. When clicking on a different language preference, I pass it thorugh url as ?lang=en, or so.
Then, the code uses GET to read the language, start session and creating a cookie named "lang".
Good so far, and actually it was working perfectly.
I was thinking about creating a subdomain, so I got it and I wanted to use the same cookie through the subdomain and i found that I could achive it using (name, value, time, '/', '.domain.com')
It never worked, so I reverted things back to normal: (name, value, time) and that is it. I used to have it with no path or domain values. Actually it is working the same way with a different website.
Long story short, after reverting changes, I am NOT able to get a session or cookie stored in the browser. I have looked in the stored info and there is no such thing as session or cookies for that site.
Answer: YES, I have cookies enabled, plus, my other sites are working perfectly and other public sites using cookies are working fine as well.
I have tried using echo to print the cookie and session value, still no luck.
Here is part of my handle code:
<?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 = 'es';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
$lang_dir = 'en';
break;
case 'es':
$lang_file = 'lang.es.php';
$lang_dir = 'es';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
This one is actually working, since I am using this and works:
<?php
$page = $_GET['page'];
if (!empty($page)) {
$content .='html/'. $lang['DIR_PATH']. '/'. $page. '.html';
include($content);
}
else {
include('html/'. $lang['DIR_PATH']. '/'. 'home.html');
}
?>
Using the link
index.php?lang=en
The previous code reads the "en" value, but as soon as I click any link, it is changed to "es".
If include file would not be working, the $lang['DIR_PATH'] would not work and no content would be displayed. Do you agree? Or am I wrong? But it does and changes language succesfully. The thing is that I can not keep the preference stored.
Thanks in advance
Wow!
Finally I double checked everything.
I got the answer already.
I realized that, somehow (don't know why), my **
php.ini
** file was modified and now was asking for a different path to save the session.
I anyone has this problem, go to php.ini file and look for
session.save_path
And either create the specific folder, or change the path showing there
Thanks for reading, anyway, for your help and your time.
Related
I'm attempting to include some code in the head.inc file that will check if the page is available in the user's location language and if so it'll redirect to that page. Obviously this isn't a great for UX but alas my client is keen.
The only issue I need to overcome is if the user has already been redirected then don't do it for every page... store the fact that the user was redirected and don't do it again until the session has ended. Something like that.
I have written the code below but a) I'm unsure this is the best way to do it and b) it seems to get stuck in a redirect loop (which I thought I'd avoided by my second session check).
I'm using the ipapi.com to check the user's location. I’m also using ProcessWire’s ‘$session’ which is effectively the same as PHP session.
if (!$session->get("lucentLightingRegion")) {
$session->set("lucentLightingSessionRedirected", "false");
if ($page->ipapiGetUserLocation()['continent_code'] === 'NA') {
$session->set("lucentLightingRegion", "NA");
if ($page->viewable('na')) {
$url = $page->localUrl('na');
$session->redirect($url);
}
} else {
$session->set("lucentLightingRegion", "INT");
if ($page->viewable('default')) {
$url = $page->localUrl('default');
$session->redirect($url);
}
}
} else {
$sessionRegion = $session->get("lucentLightingRegion");
bd($sessionRegion);
if ($page->viewable($sessionRegion) && $session->get("lucentLightingSessionRedirected") == "false") {
$url = $page->localUrl($sessionRegion);
$session->redirect($url);
$session->set("lucentLightingSessionRedirected", "true");
}
}
Calling $session->redirect($url); before $session->set("lucentLightingSessionRedirected", "true"); appears to be your issue.
Looking at the source code of $session->redirect() it executes:
header("Location: $url");
exit(0);
Which is preventing $session->set("lucentLightingSessionRedirected", "true"); from being called, resulting in the redirect loop, as lucentLightingSessionRedirected was never set to "true".
To resolve the issue, you should be able to change the order of operations.
if ($page->viewable($sessionRegion) && $session->get("lucentLightingSessionRedirected") == "false") {
$session->set("lucentLightingSessionRedirected", "true");
$url = $page->localUrl($sessionRegion);
$session->redirect($url);
}
Do Keep in mind that Wire::__call() is being used to issue Session::__redirect()
However it appears that you also have a logic flaw with the sessions and regional redirects, that may result in an undesired state. Since $page->localUrl() is using lowercase values but lucentLightingRegion is uppercase, I am not aware of how your application is designed to handle them. Additionally the default region of INT loading the default page, which may not work as desired when redirecting to $sessionRegion = 'INT'. I would need more details on how this is expected to work and if there is indeed different states for NA, na, INT and default and what is supposed to happen when none of the specified values are a viewable() page when lucentLightingRegion is set. Currently if lucentLightingRegion is set and is not viewable() or lucentLightingSessionRedirected is "true", none of the conditions will be met.
The following code is the first code in my php page.
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($_REQUEST["lang"])){ //check if different language was selected
$lang = $_REQUEST["lang"];
if($lang == "eng"){
$lang_value = 1;
}else{
$lang_value = 0;
}
setCookie('language',$lang);
setCookie('language_value',$lang_value);
header("Refresh:0; url=".$current_link);
}else{ //if different language was not selected, check if cookie is set with language value
if(isset($_COOKIE["language"])){
$lang = $_COOKIE["language"];
$lang_value = $_COOKIE["language_value"];
}else{ //if cookie with language value is not set, create it now with default language option
setCookie('language','eng');
setCookie('language_value',1);
header("Refresh:0; url=".$current_link);
}
}
The visitor can only choose one of two languages. If a language is chosen, cookies with the chosen language values are created.
If a language is not chosen, the script checks if a cookie with the language value exists, and if so, accesses the cookie values. If a cookie with the language value does not exist, cookies with the default language (English) values are created.
The page is suppose to only refresh when cookies are created and continue the rest of the code if the cookie with the language value exists. However, the page keeps on refreshing even after the cookies were created and are accessible. eg:
echo $_COOKIE["language"]; //will output the selected language value
Not sure if there is something wrong with the logic here, but any help will be appreciated.
Thanks
I managed to solve the mystery.
The problem was in the .htaccess file setup, eg:
RewriteRule diploma-golf course.php?lang=eng&courseid=15 [NC])
Since I only saw the 'diploma-golf' part at the end of the url, I never realised that the "lang" value was passed on every time the page refreshed. So because the script received the "lang" value each time, it created a new cookie each time, resulting in the infinite refreshing. Changed the if statement logic and now everything is working as it should.
Every request to the page will run this part:
if(isset($_REQUEST["lang"])){
That means that the page will be refreshed because of this unconditional part of your code:
header("Refresh:0; url=".$current_link);
What you might want to do is removing the first Header part and only keep the header part as described in your text.
It would look like so:
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($_REQUEST["lang"])){ //check if different language was selected
$lang = $_REQUEST["lang"];
if($lang == "eng"){
$lang_value = 1;
}else{
$lang_value = 0;
}
setCookie('language',$lang);
setCookie('language_value',$lang_value);
#header("Refresh:0; url=".$current_link); ### Removing this line
}else{ //if different language was not selected, check if cookie is set with language value
if(isset($_COOKIE["language"])){
$lang = $_COOKIE["language"];
$lang_value = $_COOKIE["language_value"];
}else{ //if cookie with language value is not set, create it now with default language option
setCookie('language','eng');
setCookie('language_value',1);
header("Refresh:0; url=".$current_link);
}
}
You need to add expire and path for the cookie:
setCookie('language',$lang, time() + (86400 * 30), "/"); // 86400 = 1 day
I got 2 separate PHP files on server. index.php and mailer.php. they are not connected in any ways. mailer.php is simply there to handle contact from.
Inside my index.php file using $_GET variable i store language of webpage in $lang = $_GET['lang']; which is visible in a webpage url http://nikolozasatiani.com/version2/index.php?lang=en .
How do I create a variable inside mailer.php to indicate webpage language. p.s it would be nice if u do not use require/include in your solution. it would be better to use url
As suggested in the comments, make use of $_SESSION
So typically, you would do something like this:
index.php
session_start(); // put this at the top of the page, preferably first line
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'en';
$_SESSION['lang'] = $lang;
mailer.php
session_start(); // put this at the top of the page, preferably first line
.
.
.
echo $_SESSION['lang'];
You can also make use of $_COOKIE
index.php
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'en';
/*
the cookie will expire in 1 hour
the fourth parameter ("/") makes this cookie available throughout the application
*/
setcookie("Lang", $lang, time()+3600, "/");
mailer.php
echo $_COOKIE['Lang'];
If your application is mission critical, as all decent comm apps are bound to be, the only fireproof way is to store said data serverside. All local variables / browser data is subject to user interference / disruption.
If you implement user control, and again i cant see a scenario where you wouldnt, that should be quite straightforward to include in mysql db.
I've been thinking about this for a while and I was wondering if anyone would be able to help me figure this out. I have a website, www.domain.com/page that I'm working on. The page also has the ability of having page;var=whatever at the end.I'm trying to limit how many $_POST/$_REQUEST variables I need. Is there any way to keep a session variable active only while I'm on /page so that if a user goes to /page;var=whatever the session variable is still around but not if they go to /anotherPage? Thanks!
An easy solution would be this:
session_start();
$uri = parse_url($_SERVER['REQUEST_URI']);
if (isset($_SESSION['page']) && $_SESSION['page'] != $uri['path']) {
// We went to a new page
unset($_SESSION['var']);
}
// Remember our current page.
$_SESSION['page'] = $uri['path'];
First off, please bear with the question as I am just starting out with this.
So I have the following running in an index.php:
<?php
if(isset($_COOKIE['language'])) {
if (($_COOKIE['language']) == 'en')
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URLGOESHERE\"</SCRIPT>";
elseif (($_COOKIE['language']) == 'fr')
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URLGOESHERE\"</SCRIPT>";
}
else {
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URL-FOR-COOKIE-SETTING-PAGE\"</SCRIPT>";
}
?>
The "else" statement redirect goes to the page where the user sets their language preference, and once they do so, that page sets a cookie then sends them to the proper version of the site. However, when the user goes back to this main index.php, it cannot seem to find the cookie and skips right to the "else" statement. The redirects used to be used done with PHP using 'header,' and it didn't work then either. I had read elsewhere that that could pose some troubles, so I switched it to try printing Javascript.
The weird thing is that I can find the cookie as soon as it is set, exactly where it should be, with the correct name and variable all present. I've done it exactly like the books tell me to (to my knowledge). I've tried this both in Firefox and Safari with no luck.
What did I miss?
EDIT: Here's the script that actually sets the cookie. The parameter is sent from the link via a url encode like this: <a href="setlang.php?lang=en">
<?php
$lang = urlencode($_GET["lang"]);
setcookie("language", $lang, time()+60*60*24*90, ".URL");
switch ($lang) {
case 'en':
header('Location: URLGOESHERE');
break;
case 'fr':
header('Location: URLGOESHERE');
break;
}
?>
the cookie isnt available to the script that set it.
you have to set the cookie and then on the next page load the cookie will be available to the php code
Try this code:
setcookie("language", $lang, time()+60*60*24*90, "/");
I'm uncertain what .URL was supposed to do o.o