Hướng dẫn destroy specific session php

Hướng dẫn destroy specific session php

Chào các bạn, hôm nay mình xin chia sẻ 1 mẹo nhỏ trong PHP: Destroy session_id của user khác từ server.

Tại sao cần làm việc này?

Bài toán đặt ra: Tại một thời điểm, chỉ cho phép người dùng có duy nhất 1 phiên đăng nhập trên hệ thống.
Mô tả chi tiết: Thực tế hiện tay rất nhiều hệ thống web ứng dụng bán license theo số lượng tài khoản sử dụng. Do vậy, nếu không có biện pháp ngăn chặn việc người dùng sử dụng chung tài khoản để làm việc trên hệ thống thì việc thất thoát doanh thu là khó tránh khỏi. Chính vì thế, hệ thống cần có phương án để ngăn chặn việc này, tại một thời điểm, chỉ cho phép người dùng có 1 phiên đăng nhập trên 1 thiết bị và thao tác trên hệ thống.

Cách thực hiện trên với PHP?

Mình sẽ nói tóm tắt các bước thực hiện, logic này có thể áp dụng tương tự với ngôn ngữ khác. Mục đích là sử dụng tài khoản hiện tại, để destroy session id khác trên server.
Bước 1:Commit session ID nếu nó đã tồn tại
Bước 2: Store current session id
Bước 3: Destroy session specified
Bước 4: Restore current session id

Code demo!

$session_id_to_destroy ‘nill2if998vhplq9f3pj08vjb1’;
if (session_id()) {
session_commit();
}

session_start();
$current_session_id session_id();
session_commit();

session_id($session_id_to_destroy);
session_start();
session_destroy();
session_commit();

session_id($current_session_id);
session_start();
session_commit();

?>

Xong, một mẹo khá nhỏ nhưng đôi khi lại rất cần thiết cho hệ thống của bạn. Chúc các bạn áp dụng thành công!

Tham khảo

https://www.php.net/manual/tr/function.session-destroy.php

kinhnghiemlaptrinh.com

Điều hướng bài viết

Giao lưu, chia sẻ, học hỏi – trao đổi kinh nghiệm làm việc hàng ngày

I'm currently working on a shopping cart system. It requires a user login to access the cart. So I've wrote some codes to disable access of the cart page if the user is not logged in. However, whenever I try to empty the cart, I get logged out. I just want to destroy the cart session and not the user session. Here's my code:

For the cart page:


        alert('You must be logged in.');
        window.location.href='index.php#login'
      ";
    }
?>

  

  

    

For the cart update:

query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();

    if ($results) { //we have the product info 

        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price));

        if(isset($_SESSION["products"])) //if we have the session
        {
            $found = false; //set found item to false

            foreach ($_SESSION["products"] as $cart_itm) //loop through session array
            {
                if($cart_itm["code"] == $product_code){ //the item exist in array

                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrive old info and prepare array for session var
                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
                }
            }

            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["products"] = array_merge($product, $new_product);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["products"] = $product;
            }

        }else{
            //create a new session var if does not exist
            $_SESSION["products"] = $new_product;
        }

    }

    //redirect back to original page
    header('Location:'.$return_url);
}

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
    $product_code   = $_GET["removep"]; //get the product code to remove
    $return_url     = base64_decode($_GET["return_url"]); //get return url


    foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
    {
        if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
            $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
        }

        //create a new product list for cart
        $_SESSION["products"] = $product;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}
?>