什么是session_id()和session_name()?解释它们如何在下面的代码中使用 [英] What exactly is session_id( ) and session_name( )? Explain how they are being used in the following code

查看:993
本文介绍了什么是session_id()和session_name()?解释它们如何在下面的代码中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?php
function destroy_session_and_data() {
session_start();
$_SESSION = array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time() - 2592000, '/'); 
session_destroy();
} ?>



我理解上面的代码用于终止会话,但我不能理解if条件的需要和setcookie命令。

I understand the above code is used to terminate a session but I cant understand the need for the if condition and the setcookie command.

也可以解释一下是什么是session_id()和session_name()。

Also could you please explain what exactly is session_id() and session_name().

最好的解释清楚。
感谢

A clear explanation would be most appreciated. Thanks

推荐答案

会话可以在Cookie中设置键/值对。键表示这是会话标识符,值是会话标识。默认情况下,基于PHP的网站的关键是PHPSESSID,但它可以更改。

The session works with setting a key/value pair in a cookie. The key indicates that this is the session identifier, and the value is the session id. by default for PHP based web sites the key is PHPSESSID, but it can be changed.

代码检查是否有请求传递的会话,会话(session_id()将返回会话标识符)或只是在cookie中有PHPSESSID - 并且forse浏览器通过设置过期日期来删除cookie

The code checks if there is session passed with the request, either by starting a valid session (session_id() will return the session identifier) or just there is PHPSESSID in the cookie - and forse browser to remove the cookie by setting expire date in the past

这篇关于什么是session_id()和session_name()?解释它们如何在下面的代码中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆