Is there any difference between session_destroy() then session_start() and session_regenerate_id() directly in php?

huangapple go评论79阅读模式
英文:

Is there any difference between session_destroy() then session_start() and session_regenerate_id() directly in php?

问题

The code parts you provided can be translated as follows:

```php
session_destroy();
session_start();
与
```php
session_regenerate_id();
之间的区别是什么?

它们在Cookie行为上看起来相同,但我不确定在服务器端是否有任何区别。

在成功登录后,我使用上面的解决方案,因为我看到下面的解决方案在[手册](https://www.php.net/manual/en/function.session-regenerate-id.php)上有警告。
> 目前,session_regenerate_id 在处理不稳定的网络(例如移动网络和WiFi网络)时表现不佳。因此,调用session_regenerate_id可能导致会话丢失。

我想知道我的解决方案与手册解决方案是否有什么不妥之处。
英文:

What is the difference between

session_destroy();
session_start();

and

session_regenerate_id();

They look like the same in cookie behaviour but idk if any difference in server-side.

I'm using the upper solution after successful login as I saw lower one has a warning on the manual.
> Currently, session_regenerate_id does not handle an unstable network well, e.g. Mobile and WiFi network. Therefore, you may experience a lost session by calling session_regenerate_id.

I want to know if there's any bad in my solution vs the manual solution.

答案1

得分: 0

session_regenerate_id 会保留数据并生成一个新的 ID。而 session_destroy 会销毁所有会话数据。因此,使用取决于您想要实现的目标。

session_regenerate_id() 会用一个新的会话 ID 替换当前的会话 ID,并保留当前的会话信息。

当启用 session.use_trans_sid 后,必须在 session_regenerate_id() 调用之后开始输出。否则,将使用旧的会话 ID。

来自 php.net

session_destroy() 销毁与当前会话关联的所有数据。它不会取消设置与会话关联的全局变量,也不会取消设置会话 cookie。要再次使用会话变量,必须调用 session_start()。

来自 php.net

英文:

The manual is pretty clear

session_regenerate_id will keep the data and produce a new id. Whereas session_destroy will destroy all session data. The usage, therefore, depends on what you are trying to achieve.

>session_regenerate_id() will replace the current session id with a new one, and keep the current session information.
>
>When session.use_trans_sid is enabled, output must be started after session_regenerate_id() call. Otherwise, old session ID is used.

From php.net
and

>session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

From php.net

huangapple
  • 本文由 发表于 2023年5月7日 11:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定