k0de Posted October 26, 2011 Posted October 26, 2011 Hi. I run the server program. Each user has their own login and password. But when they work at the same time, they can see each other's data. Why? And how to fix it? Quote
Administrators Farshad Mohajeri Posted October 26, 2011 Administrators Posted October 26, 2011 Hi. I run the server program. Each user has their own login and password. But when they work at the same time, they can see each other's data. Why? And how to fix it? Please give more details. How they can "see" each other's data? Where your data is stored? Which data aware components are you using? Where your data components is placed? Quote
k0de Posted October 26, 2011 Author Posted October 26, 2011 I'm in the main form of writing: if (uniEditlogin.Text ="Client1") and (uniEditPass="pass1") then PCName="Client1"; if (uniEditlogin.Text ="Client2") and (uniEditPass="pass2") then PCName="Client2"; if (uniEditlogin.Text ="Client3") and (uniEditPass="pass3") then PCName="Client3"; In another form, there is a DBGrid and a Button that saves data from the DBGrid in *.txt. For Example, the Client saves the data in a (PCName+"\data1.txt"). Client2 saves the data in a (PCName+"\data1.txt") , etc.. But if Client1 reviews the data (loads them in the form of (PCName+"\data1.txt")) he sees the contents of Client2. Quote
Administrators Farshad Mohajeri Posted October 26, 2011 Administrators Posted October 26, 2011 Is PCName a local variable in MainForm? Quote
k0de Posted October 26, 2011 Author Posted October 26, 2011 PCName is global Variable. The variable is accessible from any form. Quote
andersa@ellenshoej.dk Posted October 26, 2011 Posted October 26, 2011 All data that needs to be unique to a particular user session needs to be instanced for that session. The easiest way to accomplish this is to put variables that need to be unique for a particular user inside the Main module (UniMainModule). UniMainModule is instanced once for each user that logs on and only that user can see the data in his UniMainModule. In your case you could make PCName a public field in UniMainModule. Quote
k0de Posted October 26, 2011 Author Posted October 26, 2011 What is the relationship between the order in which the module declares a variable? Why in the UniMainModule? Quote
Administrators Farshad Mohajeri Posted October 26, 2011 Administrators Posted October 26, 2011 What is the relationship between the order in which the module declares a variable? Why in the UniMainModule? You can declare PCName in MainForm or any other Form as well, but never use a global variable. A global variable has only one instance and it is overwritten when a user assigns a new value. Quote
andersa@ellenshoej.dk Posted October 26, 2011 Posted October 26, 2011 You should think of an unigui application as a multithreaded program. Each users session is a thread in one single program and just like in a multithreaded application, global variables are shared between threads. When a user launches a session in a browser, your application is not completely cloned. There is only one physical version of your application running at all times. Each user just has a bit of reserved space inside your application that he has access to. That reserved space is the main form, the main module and all application forms that you launch subsequently. Each user gets his own fresh version of these forms when he starts a new session. Pretty much everything else is shared, unless you specifically instantiate it for a that user. Example: unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIForm; type TMainForm = class(TUniForm) private { Private declarations } public UserLocalString: string; // Each session has its own main form // so each user can only see and change // his own UserLocalString field. end; function MainForm: TMainForm; var MyGlobalString: string; // This is a typical per unit global // All users will see the same string. // If one user changes it, it changes // for all users. implementation {$R *.dfm} uses uniGUIVars, MainModule; function MainForm: TMainForm; begin Result := TMainForm(UniMainModule.GetFormInstance(TMainForm)); end; initialization RegisterMainFormClass(TMainForm); end. By the way, strings are not thread safe, so should never be shared between threads without proper locking mechanisms in place the make sure only one thread can access it at a time. 1 Quote
andersa@ellenshoej.dk Posted October 26, 2011 Posted October 26, 2011 A side note: Notice the difference between the unigui version of the Main form reference: function MainForm: TMainForm; Here is the regular VCL version: var Form1: TForm1; The unigui MainForm function is there to make sure the user gets his own main form and not some other users form. If the VCL version was used, there could only every be one main form on the entire server. So when a new user logged on, the previous user would have his main form replaced with the new users main form. That would not work very well. Quote
ibandyop Posted October 26, 2011 Posted October 26, 2011 K0de, Change the Client saves the data in a (PCName+"\data1.txt"). Client2 saves the data in a (PCName+"\data1.txt") , etc.. To aPath+'\'+UniApplication.RemoteAddress+'_data1.txt'; @Anders - very nice explanation Quote
zilav Posted October 26, 2011 Posted October 26, 2011 aPath+'\'+UniApplication.RemoteAddress+'_data1.txt'; Bad idea, it is possible to open several sessions at the same time from single host. Quote
Administrators Farshad Mohajeri Posted October 26, 2011 Administrators Posted October 26, 2011 We has Session Id which is unique: aPath + '\' + UniApplication.UniSession.SessionID + '_data1.txt'; Quote
ibandyop Posted October 27, 2011 Posted October 27, 2011 zilav and Farshad. Good points. Also I think if the data moves with the user and not the pc . Perhaps save the data with loginname.txt , even include session like Farshad has said if needed. Quote
k0de Posted October 31, 2011 Author Posted October 31, 2011 Sorry for the lack. I'll try to do as you said. Quote
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.