How to keep the cookies after browser closed?

I know this doesn’t have anything to do with you guys but How can i keep the cookies even after the browser is closed? I mean how can i edit that in php.ini? Any answer is welcomed!

Hello there,

Your browser does not automatically delete any of your cookies by default even if you will close your browser, your cookies will remain unless if you want to delete them manually or you’ve changed some browser settings or you’re using Incognito Mode which deletes cookies automatically upon exiting.

No you cannot edit your php.ini here on free hosting, full access to the php.ini file is only for premium plans.

5 Likes

hello, But on my computer it automatically got deleted and it also happen on another computer

but it does work on firefox

First of all, it’s important to note that cookies are ultimately stored by browsers, not by the server. A website can ask a browser to set a cookie and keep it for a certain time, but it’s the browser which gets final say about how long a cookie is actually kept. And if the browser auto deletes all cookies when the browser is closed, then that’s what happens.

The only thing the server can do is control the validity of PHP sessions. But PHP sessions should be considered volatile too. If you want to track a browser for a long time, your only option is a cookie with a long lifetime and hope that the browser respects it.

5 Likes

yeah it does work on firefox but not chrome.Then I would ask “How about youtube,facebook?” How are they keeping the cookies on chrome?

Like I said: cookies are controlled by the browser. I don’t know why your Chrome installation doesn’t store cookies of your own websites but does of other websites.

2 Likes

oh yeah! How about this website? when i logged in, It can keep the username even after the browser is closed.So I’m just assuming you use “ini_set()” ?

The forum doesn’t run on PHP, so it doesn’t use any PHP functions. It has it’s own session/authentication system, which uses quite long lasting cookies to keep authentication.

For starters, I’m going to assume you’re talking about PHP session cookies, not cookies in general. Custom cookies sent from the server with the setcookie function allow you to pass your own cookie lifetime. That implementation is entirely up to you.

You can use the session_set_cookie_params function to specify the cookie lifetime of sessions on your website. The default is that the sessions are valid until the browser is closed.

However, it should be noted that the contents of PHP sessions are stored on the server, and the server may clean up old sessions before the related cookie has expired. If you have your own server, you have some knobs to turn, but even then you can’t guarantee that session data will be kept for a long time.

If you don’t want that, you could implement your own session management system, where you store the session data in a place where you can keep the data for longer (e.g. a MySQL database). That way, you’re also responsible for expiring old sessions, which you can do as soon or as late as you want.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.