略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: session_abort

2024-11-14

session_abort

(PHP 5 >= 5.6.0, PHP 7, PHP 8)

session_abortDiscard session array changes and finish session

说明

session_abort(): bool

session_abort() finishes session without saving data. Thus the original values in session data are kept.

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
7.2.0 The return type of this function is bool now. Formerly, it has been void.

参见

add a noteadd a note

User Contributed Notes 4 notes

up
15
parsa dot mhn at outlook dot com
6 years ago
To better understand this function you should execute this code first :

<?php
   
// First of all choose your path , For e.g. C:/session
   
session_save_path('Your Path here !');
   
   
session_start();
   
   
// Define a Session Variable
   
$_SESSION['Key'] = 'value' ;
   
   
Var_dump(session_status() == PHP_SESSION_ACTIVE);
   
   
// Output : bool(True) , it means you have an open session !
?>

Then you should execute this code :

<?php
   
// Choose the path that you used it in first part 
   
session_save_path('Your path here');
   
   
session_start();
   
   
// If you want to close session and keep your original data in your path , you should use session_abort()
   
session_abort();
   
   
var_dump(session_status()== PHP_SESSION_ACTIVE);
   
   
// Output : bool(False) , it means your session closed .
?>

So if you have an open session , session_abort() will simply close it without effecting the external session data , so you can reload your data again from your path that you chose .
up
4
mhd dot nickz13 at gmail dot com
3 years ago
session_abort()  closes the current session and discards the changes applied to Session array in the current page  , it doesn't delete the session file 
let me explain with one example.
page 1.php :
<?php
session_start
();
$_SESSION['city']="Sydney";
echo
"<a href=\"2.php\"> page2</a>";
?>
when you open this page , a session file is created on the server (to find out where session files are saved run "echo session_save_path()" )  with the name of session_id  and the content of  the variable :

  sess_o22iabs75j93uhc7i4jf1lecjk  (file name)
  city|s:6:"Sydney";               (content)

if we go to 2.php containing this code :

<?php
session_start
();
$_SESSION['country']="Australia";
echo
session_encode();
session_abort();
session_start();
echo
"<br>".session_encode();
?>

when session_abort is executed , the session is closed and the change which here is the 'country' element of Session array is discarded . 

Output : 
city|s:6:"Sydney";country|s:9:"Australia";
city|s:6:"Sydney";
up
-8
Gulshan Kumar
3 years ago
<?php
session_start
();
if(!isset(
$_SESSION['count'])){
   
$_SESSION['count'] = 1;
}else{
   
$_SESSION['count']++;
   
session_abort();
}
echo
$_SESSION['count'];
//$_SESSION['count'] will always be 1;
//This will always echo 1 not the above code.
?>
up
-12
zh dot hailei at gmail dot com
4 years ago
demo1
<?php
session_start
();
if(!isset(
$_SESSION['count'])){
   
$_SESSION['count'] = 1;
}else{
   
$_SESSION['count']++;
}
echo
$_SESSION['count'];
//above, $_SESSION['count'] will keep increase;
?>

demo2
<?php
session_start
();
if(!isset(
$_SESSION['count'])){
   
$_SESSION['count'] = 1;
}else{
   
$_SESSION['count']++;
}
session_abort();
echo
$_SESSION['count'];
//$_SESSION['count'] will always be 1;
?>

官方地址:https://www.php.net/manual/en/function.session-abort.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3