Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cristian9509's avatar

Store into session on first time request (auth or not)

I am using the GeoIP to get the user's IP and translate it into a zipcode. I don't want to do that for every request that the user is making but rather do a one time IP to zipcode, store it into session and then when I need to use it just check if the zipcode exists inside the session.

I tried to place the code inside AppServiceProvider@boot but it does not remember it. I tried inside routes but not working as well.

Where do I need to put the code to store the zipcode into the session and have it remembered even if the user is not logged in?

0 likes
3 replies
cristian9509's avatar

@jlrdw My question is where to actually put the code to store in the session? Would it work in a service provider, route?

coder's avatar

you can store it at the time of authentication if it is required after authentication or you can do it when your home page loads like this

in your home page controller

 public function index()
    {
       //your session logic
    if(!(session()->has('zip_code')))
    {
        (session()->put('zip_code', 'YOUR_VALUE'));
    }
// rest of your logic
    }

Please or to participate in this conversation.