1

我正在使用 Nitrogen/Inets 为我的家庭使用构建一个简单的网站。只要我有一个连接到 Web 服务器的客户端,当前版本就可以按预期工作。

如果我尝试连接新用户,则 2 个会话似乎共享相同的状态变量:

  • 浏览器1:阅读索引页,点击连接,以user1登录。
  • 浏览器 2:读取索引页面,用户是 user1 --> 不正常 - 去登录并以 user2 身份输入。似乎还可以。
  • 浏览器 1:重新加载主页 --> 用户更改为 user2 --> 非常糟糕 :o(

此外,我还包括一个显示超时计数器的显示 -> 值在两个浏览器上同步。

你能告诉我我必须怎么做才能同时管理多个用户吗?我写的代码如下:

index.erl 的提取

header() ->
% if the user is not defined, show in the header a button to connect, change the title and display a reduced menu (thanks to common module)
% if the user is defined, show in the header a button to disconnect, change the title and display a complete menu (thanks to common module)
    User = wf:user(),
    Msg = case User of
        undefined -> 
            "Pas connecté";
        User -> 
            "Welcome " ++ User
    end,
    case User of
        undefined -> 
            common:header(Msg, #button { id=dcnx, text="Connexion", postback=connexion , class = rightalign});
        User -> 
            common:header(Msg, #button { id=dcnx, text="Deconnexion", postback=deconnexion , class = rightalign})
    end.


body() ->
    #container_12 { body= #grid_8 { alpha=true, prefix=2, suffix=2, omega=true, body=inner_body(wf:user()) }}.

inner_body(User) -> 
    Msg = case User of
        undefined -> "Pas connecté";
        _ -> User ++ " home"
    end,
    [
        #h2{ text= Msg },
        #p{}
    ].

event(deconnexion) ->
    % a temporary popup just to display the user name and check the function call
    wf:wire(#alert { text="Bye bye " ++ wf:user() }), 
    wf:clear_session(), % erase the session information
    wf:redirect("index"); % redisplay the index page
event(connexion) ->
    wf:redirect_to_login("login");

event(E) -> common:event(E).

login.erl 的摘录:

body() ->
% the login requires a name and a password,
% checks that {name,password} exists 
        Body = [
            #label { text="Name" },
            #textbox { id=nameTextBox, next=passwordTextBox },

            #p{},  
            #label { text="Password" },
            #password { id=passwordTextBox, next=continueButton },

            #p{},  
            #button { id=continueButton, text="Continue", postback=continue }

    ], 
    wf:wire(continueButton, nameTextBox, #validate { validators=[
        #is_required { text="Obligatoire." },
        #custom { text="utilisateur invalide.", tag=tag_name, function=fun name_validator/2 }
    ]}),

    wf:wire(continueButton, passwordTextBox, #validate { validators=[
        #is_required { text="Obligatoire." },
        #custom { text="mot de passe incorrect.", tag=tag_password, function=fun password_validator/2 }
    ]}),

    Body.

event(continue) ->
    Name = wf:q(nameTextBox), % retreive the name
    wf:user(Name),  % define as user
    wf:role(admin,is_admin(Name)), % define role
    wf:session(logged,true), % define as logged
    wf:redirect_from_login("index"); % go back to the calling page or index if not defined

event(_) -> ok.
4

1 回答 1

2

会话信息以 cookie 作为密钥存储。

您是在同一浏览器的另一个窗口中打开页面,还是打开完全独立的浏览器(IE、Chrome 和 Firefox)?如果您只是在同一浏览器中打开另一个窗口或选项卡,那么 cookie 信息肯定会被共享,从而产生您所描述的效果。

因此,请确保您启动了两个完全不同的浏览器。

您发布的代码看起来正确 - 没有什么比跨会话共享信息更突出。

于 2012-12-21T19:16:17.390 回答