Sessions and Cookies are used to store user-related data across requests. CodeIgniter provides a secure and easy-to-use API for both.
Stores data on the server
Identified by a session ID stored in a cookie
Commonly used for login/authentication
File:
Key settings:
Sessions are auto-started in CodeIgniter 4.
Access session:
Multiple values:
Check existence:
Destroy session:
Used for one-time messages (success/error).
Retrieve in view:
Check login:
Stored on the client (browser)
Has size and security limitations
Used for preferences, remember-me, etc.
| Feature | Sessions | Cookies |
|---|---|---|
| Storage | Server | Browser |
| Security | High | Lower |
| Size | Large | Small |
| Use Case | Login, auth | Preferences |
✅ Use sessions for sensitive data
✅ Use flashdata for messages
✅ Enable httponly & secure cookies
❌ Never store passwords in cookies
User authentication
Shopping cart
Language preference
Theme settings
Sessions store server-side user data
Cookies store client-side data
CodeIgniter provides secure APIs for both
Essential for authentication & UX
Take quizzes related to this topic and see where you stand!
Start Quiz Now