简体   繁体   English

如何使用 Javascript 分配 session 密钥?

[英]How can I assign a session key with Javascript?

I have a PHP program, called login.我有一个 PHP 程序,称为登录。

When the user passes something, like email and password, login.php will return a session key to the user, if the login succeeds.当用户传递一些东西,比如 email 和密码, login.php将返回一个 session 密钥给用户,如果登录成功。

And I have JavaScript code to call this function to do the login.我有 JavaScript 代码来调用这个 function 进行登录。

How can I store this session key to identify whether the user is logged in?如何存储此 session 密钥以识别用户是否已登录?

The session is stored in a cookie by the server. session 由服务器存储在 cookie 中。
You don't need to do anything on the client.您无需在客户端上执行任何操作。

a session is serverside.. not clientside.. so you cant set it through js一个 session 是服务器端..不是客户端..所以你不能通过 js 设置它

Sessions are declared manually like this:会话是这样手动声明的:

$_SESSION['user'] = $_POST['username']; // or any other variable

You can then check on the other pages with PHP like this:然后,您可以使用 PHP 检查其他页面,如下所示:

if(isset($_SESSION['user'])){

    //do this if true

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM