Skip to content Skip to sidebar Skip to footer

$_get Or $_session For Passing Variable

Hello monsters of programming. I just want to ask a question about using $_SESSION and $_GET. When to use $_GET and $_SESSION? what is the best for passing variable? Im just new to

Solution 1:

$_GET is for parameters that are needed during that specific request (or can be easily carried over to other pages), e.g.:

  • item IDs
  • current page (pagination)
  • user's profile name
  • ...

$_SESSION is for data that needs to be persisted across multiple requests, e.g.:

  • current user's ID
  • shopping carts
  • list filters
  • ...

You should use the one that better suits your use case.

That being said, I'd consider storing news_id in the session a bad thing. What if I want to edit multiple items and open multiple browser tabs? I'll end up overwriting my data. Just because you can use sessions doesn't mean you should.

Post a Comment for "$_get Or $_session For Passing Variable"