Language Session Problem


#1

I am no good at php sessions, I have been doing a whole bunch of trial and error stuff and decided to come looking for help.

Here is my situation. I have my program able to read the default language file. The problem I am having now is how to get a visitor to change the language that he needs and also have the script hoster able to set his own default upon installation.

I currently have it working where the installer has the choice of choosing the default language from the database, but not sure how to get the script to read from the database and set the default.

Here is what I have so far:

get_language.php

[code]<?

if (empty($_SESSION[‘language’][‘lid’])) {
$_SESSION[‘language’][‘lid’] = ‘1’;
}

// mysql
include “inc/dbinfo.inc.php”;

$cnn = mysql_connect ("$dblocation", “$dbusername”, “$dbpassword”) or die ('I cannot connect to the database because: ’ . mysql_error());

if (!$cnn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("$dbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT * FROM languages WHERE lid = " . $_SESSION[‘language’][‘lid’];

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo “No rows found, nothing to print so am exiting”;
exit;
}

while ($row = mysql_fetch_assoc($result)) {

$path = $row[‘lpath’];
}

mysql_free_result($result);
?> [/code]

index.php

[code]<?
include “./inc/get_language.php”;

include “./lang/”.$path."/install.lang.php"; ?>[/code]

What I can’t figure out is how to:
a] have the get_language.php file to read the variables(my table) $lang cell for the default lang. ie: $lang = ‘2’;
b] how to get the visitor to change the language on there own.

languages are as follow: lid = 1,2,3,etc…
lpath = english,french,spanish,etc…

Any input would be greatly appreciated.


#2

I figured out A on my own, stupid me, knew how to fix that problem, but I am still lost on B any ideas?


#3

Would you mind posting the answer to A then? Just in case anyone else has the same (or a similar problem).

And for B: you could have a little dropdown on your website which the user can specify their preferred language in, then on the next page, use an UPDATE query on the same session.


#4

Here is the code for those who would like it:

get_language.php

[code]<?
// mysql
include “inc/dbinfo.inc.php”;

$connection=mysql_connect ("$dblocation", “$dbusername”, “$dbpassword”) or die ('I cannot connect to the database because: ’ . mysql_error());
mysql_select_db ("$dbname");

$sql1 = mysql_query(“SELECT lang from table”) or die(mysql_error());
while($row1 = mysql_fetch_array($sql1))
{
$newlang = $row1[‘lang’];
}

if (empty($_SESSION[‘language’][‘lid’])) {
$_SESSION[‘language’][‘lid’] = $newlang;
}

// mysql
include “inc/dbinfo.inc.php”;

$cnn = mysql_connect ("$dblocation", “$dbusername”, “$dbpassword”) or die ('I cannot connect to the database because: ’ . mysql_error());

if (!$cnn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("$dbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

//your query (if it’s okay; check it with echo command - echo $sql to see if it’s good)

$sql = "SELECT * FROM languages WHERE lid = " . $_SESSION[‘language’][‘lid’];

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo “No rows found, nothing to print so am exiting”;
exit;
}

while ($row = mysql_fetch_assoc($result)) {

$path = $row[‘lpath’];
}

mysql_free_result($result);
?>[/code]


#5

Ok, how would I do that that though, would I just make it pull the IP from the visitor and stick it in the database with the language they choose and just tell

$sql1 = mysql_query("SELECT lang from table") or die(mysql_error()); while($row1 = mysql_fetch_array($sql1)) { $newlang = $row1['lang']; }

To read both a visitors table and the variables table and if visitors chosen language does not = variables display visitors choice else if empty show default language.

If that confused you, sorry, I think I may have just confused myself after reading it…lol


#6

You could set a cookie with the language code, and look for that. And if the cookie is not found, display the default language. I would advise against using the IP address as an indication, as multiple users may be using the same IP address, and users may use (or get assigned) dynamic IP addresses (which would effectively render IP-based language storage crippled).


#7

Thanks Zyppora, now I get to learn something new (never worked with cookies before…lol), time to do some googling on how to create a cookie and then figure out how to get it to interact with the language file…lol.


#8

Google is a great tool, but one that seems to find the answers quicker is http://www.php.net

This is the php manual simply do a search for $_COOKIE and you should come up with something.


#9

Better yet, there’s a whole section on it :wink:

Hmmz, on second thought, that section looks awfully scrawny. But yea, I think the key function you’ll be looking for is setcookie().