Welcome Guest, Not a member yet? Register   Sign In
How to use session()->close()?
#1

(This post was last modified: 07-17-2024, 10:04 PM by Renta Ardhana.)

Quote:Why are we telling you this? Because it is likely that after trying to find the reason for your performance issues, you may conclude that locking is the issue and therefore look into how to remove the locks …

DO NOT DO THAT! Removing locks would be wrong and it will cause you more problems!

Locking is not the issue, it is a solution. Your issue is that you still have the session open, while you’ve already processed it and therefore no longer need it. So, what you need is to close the session for the current request after you no longer need it.

Code:
<?php

$session->close();
https://www.codeigniter.com/user_guide/l...sions.html

in MyController.php
PHP Code:
class LearnSession extends BaseController
{
    public function index()
    {
        session()->set('brand''CodeIgniter4.5.3');
        session()->close();
        return redirect()->to('page2');
    }

public function 
page2()
    {
        d(session()->get());
    

result:

PHP Code:
session()->get() array (3)
⇄⧉__ci_last_regenerate => integer 1721278110
⇄_ci_previous_url 
=> string (27"http://localhost:8083/page2"
⇄brand => string (16"CodeIgniter4.5.3" 

What does session()->close() do? What does method close() mean? What is the difference between close () and remove()?
Reply
#2

1) session_close is checking to see if your in environment testing mode then it will return else it will issue a session_write_close.

2) Difference is that close is doing what I said above. remove just removes session data by item or array.

If you use AJAX then you should do a session_write_close when finished with your sessions.

Hope that clarifies it for you.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

maybe you need to execute destroy()?
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#4

close() persists the session data (e.g. it saves in the session file or the database) and closes the session.
But it does not change the existing session data (after all, $_SESSION in PHP).
Reply
#5

(This post was last modified: 07-18-2024, 01:08 AM by Renta Ardhana.)

(07-17-2024, 10:22 PM)InsiteFX Wrote: 1) session_close is checking to see if your in environment testing mode then it will return else it will issue a session_write_close.

2) Difference is that close is doing what I said above. remove just removes session data by item or array.

..

Hope that clarifies it for you.

@InsiteFX thank you, I got the difference now. 1. Closing the Session "process" vs 2. Removing the data/item/array.

(07-17-2024, 10:22 PM)InsiteFX Wrote: If you use AJAX then you should do a session_write_close when finished with your sessions.

Does it mean that session()->close() can only improve performance for AJAX?

What about in the context of non-AJAX requests (normal page reload)? Can session()->close() also improve performance in this case; as per documentation said that?

(07-17-2024, 10:43 PM)ozornick Wrote: maybe you need to execute destroy()?

@ozornick thank you, I will destroy session when user logout, right?

 At first, I thought close() was a new feature in CI4 to enhance the destroy method, but it gues I was wrong."

(07-17-2024, 11:40 PM)kenjis Wrote: close() persists the session data (e.g. it saves in the session file or the database) and closes the session.
But it does not change the existing session data (after all, $_SESSION in PHP).

I understand now what session->close() do (and don't).

As stated in the documentation, it says that closing the session is a best practice for improving performance related to locking/unlocking the session. Does this also apply to non-AJAX requests, regarding @InsiteFX comment above?
Reply
#6

@Renta Ardhana, The best way to understand things like the session methods etc, is to view the source files
in .\system folder.

If you look at the methods you should be able to understand what the developers are doing and how the methods work.

When I first started using CI 1.6.2 back in 2014 I sat down on the weekends and just kept reading the CodeIgniter Users Guide
over and over to get a very good under standing of what was going on etc. I also skimmed through the .\system files to understand
what they were doing.

Just my personal opinion.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Read https://www.codeigniter.com/user_guide/l...oncurrency
Reply
#8

(07-18-2024, 02:53 AM)InsiteFX Wrote: @Renta Ardhana, The best way to understand things like the session methods etc, is to view the source files
in .\system folder.

If you look at the methods you should be able to understand what the developers are doing and how the methods work.

When I first started using CI 1.6.2 back in 2014 I sat down on the weekends and just kept reading the CodeIgniter Users Guide
over and over to get a very good under standing of what was going on etc. I also skimmed through the .\system files to understand
what they were doing.

Just my personal opinion.

@InsiteFX thanks for sharing your experience.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB