claudio_sti
Posts: 9
|
| Posted: 08/11/2009, 7:28 AM |
|
Hi,
I would like to switch to another db based who is logging in. Login name, password and the database-to-connect-to are stored in a "common" db. All databases have exactly the same structure but different languages and a lot of region specific content. It was a requirement to manage the databases independently.
I've reviewed quite a few posts, but nothing works for me. I've also followed this approach: http://forums.codecharge.com/posts.php?post_id=105138
When I'm using variables as connection parameters in common.php the login doesn't proceed (without error message). If I'm simply overwriting the parameters in $CCConnectionSettings after CCLoginUser(), I continue being connect to the initial db.
I'm not an experienced in CCS user. Maybe I'm missing something very basic here. I really hope someone can help.
Many thanks in advance,
Claudio
|
 |
 |
tcb
Posts: 34
|
| Posted: 08/11/2009, 3:13 PM |
|
Not quite sure exactly what you're trying to accomplish, but you may certainly replace the entire Connection Settings block of Common.php with values defined in an off-site include file. However, you do have to let CCS build its datasources the way it wants to, then alter them.
//Connection Settings @0-D5948BED
require_once("Common.inc.php");
//End Connection Settings
(Remember that, after the operation, Common.php won't be automatically regenerated any longer.)
Common.inc.php contains defined values:
$CCConnectionSettings = array (
"mct_main" => array(
"Type" => DB1_TYPE,
"DBLib" => DB1_LIB,
"Database" => DB1_NAME,
"Host" => DB1_HOST,
"Port" => DB1_PORT,
"User" => DB1_USER,
"Password" => DB1_PASSWORD,
"Persistent" => false,
"DateFormat" => array("yyyy", "-", "mm", "-", "dd", " ", "HH", ":", "nn", ":", "ss"),
"BooleanFormat" => array(1, 0, ""),
"Uppercase" => false
),
"mct_udb0" => array(
"Type" => DB2_TYPE,
"DBLib" => DB2_LIB,
"Database" => DB2_NAME,
"Host" => DB2_HOST,
"Port" => DB2_PORT,
"User" => DB2_USER,
"Password" => DB2_PASSWORD,
"Persistent" => false,
"DateFormat" => array("yyyy", "-", "mm", "-", "dd", " ", "HH", ":", "nn", ":", "ss"),
"BooleanFormat" => array(1, 0, ""),
"Uppercase" => false
)
);
and so on, and so on.
(I use the above method on literally all projects, because I'm developing on Windows for Linux servers; the Linux users aren't supposed to have access to the databases, which all have different port numbers and socket files. So there's a different include file for each server, but Common.php only has to be set up once.)
So that's that part.
WRT differential users<->databases, a CCS user doesn't have anything to do with the database user. You can get the CCS user by calling CCGetUserID(). You may then use that to set each form's DataSource. (You could even use it to switch the defines in the above configuration example.) But this won't change the database privileges; you have to do that separately.
Hope this helps.
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 08/12/2009, 6:22 PM |
|
Couple ways to accomplish this for a multi language impllementaion.
To pint to en entirely different databas you just need to create a second connection for the other DB in the settings screen.
Then just change to that connection globally based on who logged in.
I however think that is an exptreemly dificult way to do this. Not to mention the code to ensure DB switching based on user. but all the common tables such users table and site configuration tables would have to be maintained in duplicate.
I think an easier way is to switch to language specific tables in the same data base based on language.
Here is an example of what I implemented with that technique.
http://realtest.biz/TAXIFEE/
You wil notice when you switch languages the content changes with the language allowing different content per language selected.
Hope that helps.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
claudio_sti
Posts: 9
|
| Posted: 08/13/2009, 11:41 PM |
|
Thanks a lot for these quick and very helpful replies. It's always challenging to describe a problem in a few lines of text but it was understood quite correctly. To be a little more precise: After the login page I want to switch to another database which will then be the only database to use throughout all pages. In addition, the function needs to dynamic: The administrator should be able add new users and assign them to a default database - maybe even create new databases. For organizational reasons I have to have separate DBs
Thanks for your proposed solutions. I've tried similar things before. I like the idea of simply adding more elements to the Connection Settings. It does work for me (for one connection). What I don't understand is how you can handle the different names without touching too much of CCS code. The connection name is CC hard coded all over the files. In this case, you have to use CCS to add new connections whose names correspond to the connection names in your Common.inc.php? If I understood you correctly, this would mean I had to do organize the db change in each form and record. Besides the considerable effort to do this in a large project, I would also loose flexibility (you need CCS to add another DB). So I'm not sure if this is the way to go.
The best way seems really to change the defines in common.inc.php based who is logged in at that time and overwrite the parameters of one single connection. Apart from all the country specific databases, I will have to have one common db, where the user information form all countries is stored (this can be done automatically when maintaining the logins). This results in some little redundancy… but ok. On the other hand, every language/country/region can change the content and configuration of their page individually - which is a good thing in our case.
Here’s sample code of what I wrote in common.inc.php
<?php
define("RelativePath", ".");
define("PathToCurrentPage", "/");
define("FileName", "Commmon.inc.php");
include_once(RelativePath . "/Common.php");
$db = " DB1_COMMON";
$user = CCGetSession("UserLogin");
//If user is already logged in, read the user-specific db from common db
if($user){
$con = mysql_connect( "localhost","root","");
if ($con){
mysql_select_db($db);
$result = mysql_result(mysql_query("SELECT DefaultDB FROM login WHERE username = '" . $user . "'"),0,0);
if ($result)
$db = $result;
mysql_close($con);
}
}
switch ($db){
case "DB1_NAME_Russian":
$CCConnectionSettings = array (
" MyConnectionName" => array(
"Type" => "MySQL",
"DBLib" => "MySQL",
"Database" => " DB1_NAME_Russian",
"Host" => "localhost",
"Port" => "3306",
"User" => "root",
"Password" => "",
"Encoding" => array("", "utf8"),
"Persistent" => false,
"DateFormat" => array("mm", "/", "dd", "/", "yyyy", " ", "HH", ":", "nn", ":", "ss"),
"BooleanFormat" => array("true", "false", ""),
"Uppercase" => false
)
);
break;
default:
$CCConnectionSettings = array (
" MyConnectionName " => array(
"Type" => "MySQL",
"DBLib" => "MySQL",
"Database" => " DB1_COMMON",
"Host" => "localhost",
"Port" => "3306",
"User" => "root",
"Password" => "",
"Encoding" => array("", "utf8"),
"Persistent" => false,
"DateFormat" => array("mm", "/", "dd", "/", "yyyy", " ", "HH", ":", "nn", ":", "ss"),
"BooleanFormat" => array("true", "false", ""),
"Uppercase" => false
)
);
echo "user not known";
break;
}
?>
The code works well under 'lab conditions'. The problem I'm having is that the statement '$user = CCGetSession("UserLogin");' doesn’t give any result when it gets called during the regular connection process (although there is a user logged in). If I access the file directly (typing …./common.inc.php in the browser) the function does work correctly. I think if I solved that problem, I’m almost there. Any ideas why this does not work or how I could accomplish this otherwise?
Thanks again for your great support!
Claudio
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 08/14/2009, 2:06 AM |
|
Like I mentioned earlier changing the DB connection on the fly is by far the most complicated and difficult method you could choose do do this.
The easiest way is to change the DB table to the correct language table as In the link I showed you above.
Doing ot that way only takes 1 line of code in the before select event based on the users language selection.
In other words. it is very easy to do it that way, only 1 line of code and you are done.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 08/14/2009, 7:12 AM |
|
BTW This might help
http://forums.yessoftware.com/posts.php?post_id=103166
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
claudio_sti
Posts: 9
|
| Posted: 08/14/2009, 7:51 AM |
|
Many thanks. I've seen this great tutorial before and it helped me a lot. Apart form the database issue, my page is already running multilingual. Unfortunately, I cannot do it exactly the way you're describing. The reason why I'm perusing the 'most complicated method possible' is a) The database (and with this language, and also content) is actually linked to the user and not directly to the language locale. b) It is a requirement to have separate databases so adding language specific tables to one single database is not possible.
The missing piece in my solution is only to get the username into the common.ini.php file, then I'm done. But again, If you have ideas how to do it in a different way while meeting the conditions I mentioned I'd be happy to hear them.
Bye, Claudio
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 08/14/2009, 9:27 AM |
|
Hi
I am confused. I assume users do not each have a unkown language. So in the users table you must have a field that remembers the users language so the site will be in that language.
CCS has very powerful multi language support built in using the standard variables such as locale.
You do not need to get the users name in common.
All you need to do is in the on Click event for your login button add some custom code to get the users language from the database then add to the $Redirect string "?locale=de" or what ever language you are using. The site will load with that language and be using the standard mechanism built into CCS. Then you can do the oneline table switch in the before execute select event
Don't forget to do the same one-line table switch for before execute insert, update, delete as well if the user will be updating tables
Try it this way. You will find it much easier tyo code and maintain and your CCS project wil not contain no modiofications to CCS generated code
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
tcb
Posts: 34
|
| Posted: 08/16/2009, 3:51 PM |
|
When I really, really need to do this I make a tiny second CCS project just for login using one master user database. After login, you have your user ID and can redirect to the main project(s). For extra-tasty, store each user's database privileges and credentials in the common user table and pass them to the other project(s) as POST parameters. Or, just use different but same-named include files.
Be sure to use a secure connection throughout if you do that!
|
 |
 |
|