Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关闭会话并启动新会话_Php_Session - Fatal编程技术网

Php 关闭会话并启动新会话

Php 关闭会话并启动新会话,php,session,Php,Session,我正在测试PHP会话中安全检查的实现。我可以成功地检测会话是否是从其他IP地址启动的,并且我可以成功地启动新会话。但是,旧会话中的数据会被复制到新会话中!如何启动空白会话,同时为其合法所有者保留以前的会话数据 这是我迄今为止的代码,经过多次失败的尝试: <?php // Security check if( isset($_SESSION['ip_address']) && $_SERVER['REMOTE_ADDR']!=$_SESSION['ip_address']

我正在测试PHP会话中安全检查的实现。我可以成功地检测会话是否是从其他IP地址启动的,并且我可以成功地启动新会话。但是,旧会话中的数据会被复制到新会话中!如何启动空白会话,同时为其合法所有者保留以前的会话数据

这是我迄今为止的代码,经过多次失败的尝试:

<?php

// Security check
if( isset($_SESSION['ip_address']) && $_SERVER['REMOTE_ADDR']!=$_SESSION['ip_address'] ){
    // Check failed: we'll start a brand new session
    session_regenerate_id(FALSE);
    $tmp = session_id();
    session_write_close();
    unset($_SESSION);
    session_id($tmp);
    session_start();
}

// First time here
if( !isset($_SESSION['ip_address']) ){
    $_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR'];
    $_SESSION['start_date'] = new DateTime;
}
使用此

unset($_SESSION['ip_address'])
而不是“取消设置($\会话)” 您还可以使用session\u destroy。

只需在重置当前会话的
$\u session之后调用即可:

if (isset($_SESSION['ip_address']) && $_SERVER['REMOTE_ADDR']!=$_SESSION['ip_address']) {
    // Check failed: we'll start a brand new session
    session_regenerate_id(FALSE);
    session_unset();
}

当新用户连接到服务器时,脚本应该只能访问该用户的会话变量。您需要将其他信息存储在散列会话变量中,以验证会话是否未被劫持。如果它被顶起,没有理由启动新会话,可能只是退出脚本并发出警告

以下是很多人在会话中使用的指纹识别功能:

function fingerprint() {
    $fingerprint = $server_secure_word;
    $fingerprint .= $_SERVER['HTTP_USER_AGENT'];
    $blocks = explode('.', $_SERVER['REMOTE_ADDR']);
    for ($i=0; $i<$ip_blocks; $i++) {
        $fingerprint .= $blocks[$i] . '.';
    }
    return md5($fingerprint);
}
函数指纹(){
$fingerprint=$server\u secure\u word;
$fingerprint.=$\服务器['HTTP\用户\代理'];
$blocks=explode('.',$服务器['REMOTE\u ADDR']);

对于($i=0;$i
会话,销毁
销毁会话数据。例如,
session_start();
$_SESSION["test"] = "test";
session_write_close();
session_start();
// now session is write to the session file
// call session_destroy() will destroy all session data in the file.
session_destroy();
// However the you can still access to $_SESSION here
print_r($_SESSION);
// But once you start the session again
session_start();
// all session data is gone as the session file is now empty
print_r($_SESSION);
将输出

array([test] => "test")array()

@Álvaro G.Vicario-session_destroy?或session_unregister-@ajreal,你是对的:虽然session_destroy()的名称表明它删除了会话数据,但它没有。我正在测试它,以了解它的确切功能。请在回答中详细说明。session_unregister()是不推荐的,顺便说一句,@lvaro G.Vicario-aiya,5.3?…还有一个,session_unset-@ajreal,nope,5.2;您在一小时前删除了标记:)手册不鼓励这样做:如果使用$_session[…],使用unset()取消注册会话变量,即unset($_session['varname])@lvaro G.Vicario:
取消注册会话变量[…]”和“不要用
取消设置($\u会话)
[…]取消设置整个
$\u会话”
”。没有类似的说法*‍不要使用
session\u unset
重置整个
$\u会话
‍*‍‍.刚刚测试:session_unset()删除会话数据。我不能在第一个会话中使用它,因为我想保留它,但我想我可以在第二个会话中使用它。听起来不合逻辑,但它应该可以工作。@Ivaro G.Vicario:
session\u regenate\u id
基本上创建一个新会话,并将会话数据从旧会话复制到新会话。因此调用
session\u unset
只会影响新会话,而不会影响旧会话。完成此代码后,您仍然需要调用
session\u start()
启动一个新会话,对吗?或者它实际上是重置的吗?我不想因为用户的ISP或DHCP服务器更改了他的IP地址而对用户无礼;-)我已经好几年没有研究过这个问题了,但是如果我没记错的话,
会话销毁()的问题是它不会删除文件(即,您仍然可以使用旧ID打开旧会话)。我可以再次测试。
array([test] => "test")array()