w3resource

PHP cookies and sessions: Exercises, Practice, Solutions


This resource offers a total of 80 PHP Cookies and Sessions problems for practice. It includes 16 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


The following exercises cover various aspects of working with cookies and sessions in PHP. These include setting and retrieving values, session management, session security, and common usage scenarios.


1. Set "username" Cookie with Expiry

Write a PHP script to set a cookie named "username" with the value "Gulnara Serik" and an expiration time of one hour.

Click me to see the sample solution


2. Retrieve "username" Cookie

Write a PHP script to retrieve and display the value of the cookie named "username".

Click me to see the sample solution


3. Delete "username" Cookie

Write a PHP script to delete a cookie named "username".

Click me to see the sample solution


4. Set "userid" Session Variable

Write a PHP script to set a session variable named "userid" with the value 10020.

Click me to see the sample solution


5. Retrieve "userid" Session Variable

Write a PHP script to retrieve and display the value of the session variable "userid".

Click me to see the sample solution


6. Destroy Session and Unset Variables

Write a PHP script to destroy a session and unset all session variables.

Click me to see the sample solution


7. Set Secure Cookie Over HTTPS

Write a PHP script to set a secure cookie that can only be transmitted over an encrypted connection.

Click me to see the sample solution


8. Check for "visited" Cookie

Write a PHP script to check if a cookie named "visited" exists. If it does, display a welcome message; otherwise, display a default message.

Click me to see the sample solution


9. Store Array in Session Variable

Write a PHP script to store an array of user preferences in a session variable.

Click me to see the sample solution


10. Retrieve Session User Preferences

Write a PHP script to retrieve and display user preferences stored in the session variable.

Click me to see the sample solution


11. Session Timeout After 30 Minutes

Write a PHP script to set a session timeout after 30 minutes of inactivity.

Click me to see the sample solution


12. Display Number of Active Sessions

Write a PHP script to display the number of active sessions on the server.

Click me to see the sample solution


13. Limit Maximum Concurrent Sessions

Write a PHP script to limit the maximum number of concurrent sessions for a user to 3.

Click me to see the sample solution


14. Regenerate Session ID to Prevent Fixation

Write a PHP script to regenerate the session ID to prevent session fixation attacks.

Click me to see the sample solution


15. Display Last Session Access Time

Write a PHP script to display the last time the session was accessed by the user.

Click me to see the sample solution


16. Set Cookie and Session Variable with Same Name

Write a PHP script to set a cookie and a session variable with the same name. Display their values to compare.

Click me to see the sample solution


PHP Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.