Can I delete a session on the server via session ID in PHP?

Source: Internet
Author: User
Tags phpinfo vars
Want to achieve a kick out of the member's function, but how to do in PHP, my idea is to record each member login session ID, to kick a member of the time through this ID to the server to remove this session, to kick the effect, but do not know whether PHP can pass the session ID Delete a session, ask you!!!


Reply to discussion (solution)

Since you can record the session ID of each member login (obviously to be saved in the database)
Then you just change the session file to database mode.
If you want to play, delete the corresponding record.

Since you can record the session ID of each member login (obviously to be saved in the database)
Then you just change the session file to database mode.
If you want to play, delete the corresponding record.


Thank you moderator answer, can not be said in detail, I am now the session ID is stored in the database, the key is the client ah, such as you in the background to obtain a member of the session ID, how to the client side of the session to expire, so you can kick out members, What does the moderator mean by deleting the corresponding records? Shortly after using PHP, laughed at.

Session is stored in the server, you delete after the client session will not be able to get to log in again.

Session is stored in the server, you delete after the client session will not be able to get to log in again.


I want to know how to do ah, how to delete, check the function only Session_destroy (), this function has no parameters, can only delete this account login session, to delete the other through SessionID how to delete it

Moderator means to save the session in DB instead of the file, because your session ID has been saved in the DB, you can save the session in DB, so easy to operate. The corresponding DB record is then deleted according to the session ID.

If the session is stored in a file, the session ID can also be used to delete the file implementation.
Session file is saved in Session.save_path, usable phpinfo (); Find the

Session_Start (); $_session[' test '] = 1;echo session_id ();

After the above code is executed, for example session_id () returns IAK6L6LURG6O63A00TSDI0A4M5
The SESS_IAK6L6LURG6O63A00TSDI0A4M5 file is generated in Session.save_path, which is the session file.
Then, according to IAK6L6LURG6O63A00TSDI0A4M5 this session ID to perform the delete file operation is possible.

Moderator means to save the session in DB instead of the file, because your session ID has been saved in the DB, you can save the session in DB, so easy to operate. The corresponding DB record is then deleted according to the session ID.

If the session is stored in a file, the session ID can also be used to delete the file implementation.
Session file is saved in Session.save_path, usable phpinfo (); Find the

Session_Start (); $_session[' test '] = 1;echo session_id ();

After the above code is executed, for example session_id () returns IAK6L6LURG6O63A00TSDI0A4M5
The SESS_IAK6L6LURG6O63A00TSDI0A4M5 file is generated in Session.save_path, which is the session file.
Then, according to IAK6L6LURG6O63A00TSDI0A4M5 this session ID to perform the delete file operation is possible.


Thanks, I'll try, thank you.

Since you can record the session ID of each member login (obviously to be saved in the database)
Then you just change the session file to database mode.
If you want to play, delete the corresponding record.


Moderator if you follow the way that I deleted a user's session ID, the user every time access to a page is also to query the database, to determine the session ID is still there, if not exist to prove that the T downline, is this it?

Moderator means to save the session in DB instead of the file, because your session ID has been saved in the DB, you can save the session in DB, so easy to operate. The corresponding DB record is then deleted according to the session ID.

If the session is stored in a file, the session ID can also be used to delete the file implementation.
Session file is saved in Session.save_path, usable phpinfo (); Find the

Session_Start (); $_session[' test '] = 1;echo session_id ();

After the above code is executed, for example session_id () returns IAK6L6LURG6O63A00TSDI0A4M5
The SESS_IAK6L6LURG6O63A00TSDI0A4M5 file is generated in Session.save_path, which is the session file.
Then, according to IAK6L6LURG6O63A00TSDI0A4M5 this session ID to perform the delete file operation is possible.


Use unlink ($myfile), delete the file when the prompt does not have permission, how to get permission to delete the session file?

Session_set_save_handler to database mode
The related operation is done automatically by PHP without you having to write code

File mode session temporary file is locked during user operation, you only use between user two operations
session_id (user's SessionID);
Session_unset ();
Session_write_close ();
To delete the temporary file for the SessionID

Session_set_save_handler to database mode
The related operation is done automatically by PHP without you having to write code

File mode session temporary file is locked during user operation, you only use between user two operations
session_id (user's SessionID);
Session_unset ();
Session_write_close ();
To delete the temporary file for the SessionID


Understand, I made a mistake, should delete the other session ID file, not the session ID being accessed, as well as the moderator to ask you, you said
Session_set_save_handler to the database mode, the related operation is automatically completed by PHP, what is the meaning, I mean, if I want to kick off a user, the database of the user's session ID value is set to NULL, And then kicked out of this user to visit other pages, and then query his session ID, if the empty is t off, I now understand is this, do not know right, is this, moderator, hope pointing, grateful!!!

Yes, well, without a record of SessionID, you have to log back in.

Yes, well, without a record of SessionID, you have to log back in.


Thank you for the moderator's reply, you can paste the

Yes, well, without a record of SessionID, you have to log back in.


Moderator sorry to ask you again, such a PHP file how to use it
/*============================ File Description ========================================
@filename: session.class.php
@description: Database Save Online User session, realize online user function!
@notice: The session expires one hours because our site is logged in using a cookie (valid for 1 hours).
So we only log the time that the user logged in, not the update once.
The action to delete a session record in the database occurs when the user times out and executes the file or exits normally (session_destory)
@database: Database:sessions Field:sessionid (Char32), UID (int10), Last_visit (int10)
=============================================================================
*/
Class Session {
Private $db;
Private $lasttime =3600;//Time-out: one hours
Function session (& $DB) {
$this->db = & $db;
Session_module_name (' user '); Session file Save way, this is necessary! Unless you set the php.ini file in the
Session_set_save_handler (
Array (& $this, ' Open '),//execute at Run session_start ()
Array (& $this, ' close '), executed when the script executes or calls Session_write_close () or Session_destroy (), which is executed after all sessions have completed
Array (& $this, ' read '), executed at Run Session_Start (), because at Session_Start, the current session data is read
Array (& $this, ' write '),//This method executes when the script ends and uses session_write_close () to force the session data to be committed
Array (& $this, ' Destroy '),//execute at Run Session_destroy ()
The array (& $this, ' GC ')//execution probabilities are determined by the values of session.gc_probability and session.gc_divisor, and the timing is after Open,read, session_ Start will execute Open,read and GC successively
);
Session_Start (); This is also necessary, open the session, must be executed behind the Session_set_save_handler
}
function unserializes ($data _value) {
$vars = Preg_split (
'/([a-za-z_x7f-xff][a-za-z0-9_x7f-xff]*) |/',
$data _value,-1, Preg_split_no_empty |
Preg_split_delim_capture
);
for ($i = 0; Isset ($vars [$i]); $i + +) {
$result [$vars [$i + +]] = Unserialize ($vars [$i]);
}
return $result;
}
function open ($path, $name) {
return true;
}
function Close () {
$this->GC ($this->lasttime);
return true;
}
function Read ($SessionKey) {
$sql = "Select UID from sessions WHERE session_id = '". $SessionKey. "' Limit 1 ';
$query = $this->db->query ($sql);
if ($row = $this->db->fetch_array ($query)) {
return $row [' uid '];
}else{
Return "";
}
}
function Write ($SessionKey, $VArray) {
Require_once (mroot.dir_ws_classes. ' Db_mysql_class.php ');
$db 1=new dbcom ();
Make a connection to the database ... now
$db 1->connect (Db_server, Db_server_username, Db_server_password, db_database);
$db 1->query ("Set names UTF8");
$this->db= $db 1;
$SessionArray = Addslashes ($VArray);
$data = $this->unserializes ($VArray);
$sql 0 = "Select uid from sessions WHERE session_id = '". $SessionKey. "' Limit 1";
$query 0 = $this->db->query ($sql 0);
if ($this->db->num_rows ($query 0) <=0) {
if (Isset ($data [' webid ']) &&!empty ($data [' webid ']) {
$this->db->query ("INSERT INTO ' sessions ' set ' session_id ' = ' $SessionKey ', uid= '". $data [' WebID ']. "', last_visit= '. Time (). "'");
}
return true;
}else{
/* $sql = "Update ' sessions ' Set";
if (Isset ($data [' webid '])) {
$sql. = "UID = '". $data [' WebID ']. "',";
}
$sql. = "' last_visit ' = null"
. "Where ' session_id ' = ' $SessionKey '";
$this->db->query ($sql); */
return true;
}
}
function Destroy ($SessionKey) {
$this->db->query ("Delete from ' sessions ' where ' session_id ' = ' $SessionKey '");
return true;
}
Function GC ($lifetime) {
$this->db->query ("Delete from ' Sessions ' where Unix_timestamp (now ())-' last_visit ' > '". $this->lasttime. "'");
return true;
}
}
?>
Other pages to use this technology, is not every page to introduce this class and create a session class object, each session will not be called when the function destroy and GC function.

Many adults, save to the database the best


Moderator means to save the session in DB instead of the file, because your session ID has been saved in the DB, you can save the session in DB, so easy to operate. The corresponding DB record is then deleted according to the session ID.

If the session is stored in a file, the session ID can also be used to delete the file implementation.
Session file is saved in Session.save_path, usable phpinfo (); Find the

Session_Start (); $_session[' test '] = 1;echo session_id ();

After the above code is executed, for example session_id () returns IAK6L6LURG6O63A00TSDI0A4M5
The SESS_IAK6L6LURG6O63A00TSDI0A4M5 file is generated in Session.save_path, which is the session file.
Then, according to IAK6L6LURG6O63A00TSDI0A4M5 this session ID to perform the delete file operation is possible.


Use unlink ($myfile), delete the file when the prompt does not have permission, how to get permission to delete the session file?



I tested yes, did you delete the session file you are using? Of course not.



Moderator means to save the session in DB instead of the file, because your session ID has been saved in the DB, you can save the session in DB, so easy to operate. The corresponding DB record is then deleted according to the session ID.

If the session is stored in a file, the session ID can also be used to delete the file implementation.
Session file is saved in Session.save_path, usable phpinfo (); Find the

Session_Start (); $_session[' test '] = 1;echo session_id ();

After the above code is executed, for example session_id () returns IAK6L6LURG6O63A00TSDI0A4M5
The SESS_IAK6L6LURG6O63A00TSDI0A4M5 file is generated in Session.save_path, which is the session file.
Then, according to IAK6L6LURG6O63A00TSDI0A4M5 this session ID to perform the delete file operation is possible.


Use unlink ($myfile), delete the file when the prompt does not have permission, how to get permission to delete the session file?



I tested yes, did you delete the session file you are using? Of course not.


Indeed, I delete is in use, no wonder no, session_set_save_handler this method should be the best, I try

Session_set_save_handler to database mode
The related operation is done automatically by PHP without you having to write code

File mode session temporary file is locked during user operation, you only use between user two operations
session_id (user's SessionID);
Session_unset ();
Session_write_close ();
To delete the temporary file for the SessionID



$con = mysql_connect ("127.0.0.1", "root", "111111");

mysql_select_db ("session");

function open ($save _path, $session _name) {
return (true);
}

function Close () {
return (true);
}

function Read ($id) {
if ($result = mysql_query ("SELECT * from session where id= ' $id '")) {

if ($row = mysql_fetch_array ($result)) {
return $row ["Data"];
}
} else {
Return "";
}
}

function Write ($id, $sess _data) {

$sql = "SELECT * FROM session WHERE ID = '". $id. "' Limit 1 ';
$check =mysql_query ($sql);
$total =mysql_num_rows ($check);
$result = false;
if ($total <=0) {
$sql = "INSERT INTO ' session ' Set ' id ' = ' $id ', Data= '". $sess _data. "', last_visit= '". Time (). "'";
mysql_query ($sql);
}else{
$sql = "Update session set Data= ' $sess _data ', last_visit= '". Time (). "' Where id= ' $id '";
$result = mysql_query ($sql);
}

if ($result ==true) {
return true;
} else {
return false;
}
}

function Destroy ($id) {

if ($result = mysql_query ("Delete * from session where id= ' $id '")) {
return true;
} else {
return false;
}
}

Function GC ($maxlifetime) {
$sql = "Delete from ' Session ' where Unix_timestamp (now ())-' last_visit ' > ' 60 '";
$result = mysql_query ($sql);
if ($result = = True) {
Echo $sql;
return true;

} else {
echo "Error";
return false;

}

}

Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");
@ini_set (' session.gc_probability ', 1);
@ini_set (' Session.gc_divisor ', 2);
@ini_set (' Session.gc_maxlifetime ', 60);
Session_Start ();

? >//file Name session_user_start.php
Moderator for guidance, I wrote 2 pages of testing,
1.php

Include ' session_user_start.php ';

$_session[' Test ' = "ok la";

echo "Init OK";

?>

The other One
2.php

Include ' session_user_start.php ';
echo $_session[' test '];
?>
can access, but after 60 seconds to access the 2.php Discovery GC that function did not call, ask how to do in the session failure, Access 2.php let the GC function to execute it

GC that can write a crontab implementation.
http://blog.csdn.net/fdipzone/article/details/7263361

GC that can write a crontab implementation.
http://blog.csdn.net/fdipzone/article/details/7263361


GC has been called, but found that other pages introduced include ' session_user_start.php ';
echo $_session[' test '], found in the previous page assignment session to the introduction of the session of the page, $_session value is empty, strange

Know what the reason is that when read to return to the session data, so that when introduced to other pages will be the session data

Use static array and session to save the user name + logon time, verify the time to take the session data and array data comparison, is it feasible

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.