PHP Sessions & Cookies

What are Sessions?

Sessions store data on the server for a specific user.

They are used to keep track of users across pages.

Starting a Session

Loading...
Output:

Setting Session Variables

Loading...
Output:

Accessing Session Variables

Loading...
Output:

Destroying a Session

Loading...
Output:

What are Cookies?

Cookies store data in the user's browser.

They can persist after the browser is closed.

Setting a Cookie

Loading...
Output:

Accessing Cookies

Loading...
Output:

Deleting a Cookie

Loading...
Output:

Sessions vs Cookies

  • Sessions → stored on server, more secure
  • Cookies → stored in browser, less secure
  • Sessions → temporary
  • Cookies → can persist longer

Full Session Example

Loading...
Output:

Full Cookie Example

Loading...
Output:

Why Sessions & Cookies are Important

  • Login systems
  • User preferences
  • Tracking users

Common Mistakes

  • Forgetting session_start()
  • Accessing cookies before setting them
  • Storing sensitive data in cookies

Practice

Create a session that stores a username and display it on another page.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic