Practice 50 PHP Cookies and Sessions Programming Questions, TechnoVlogs

Practice 50 PHP Cookies and Sessions Programming Questions


Q1. Write a PHP script to set a cookie named user with the value John and an expiration time of 1 hour.  
  Input: None  
  Expected Output: Cookie user set successfully.

Q2. Write a PHP script to retrieve the value of a cookie named user.  
  Input: Cookie user exists with the value John.  
  Expected Output: Cookie value: John

Q3. Write a PHP script to check if a cookie named language exists.  
  Input: Cookie language does not exist.  
  Expected Output: Cookie language does not exist.

Q4. Write a PHP script to delete a cookie named cart.  
  Input: Cookie cart exists.  
  Expected Output: Cookie cart deleted successfully.

Q5. Write a PHP script to set a session variable named username to Alice.  
  Input: None  
  Expected Output: Session variable username set to Alice.

Q6. Write a PHP script to retrieve the value of a session variable named username.  
  Input: Session variable username exists with the value Alice.  
  Expected Output: Session value: Alice

Q7. Write a PHP script to check if a session variable named loggedIn exists.  
  Input: Session variable loggedIn does not exist.  
  Expected Output: Session variable loggedIn does not exist.

Q8. Write a PHP script to unset a session variable named email.  
  Input: Session variable email exists.  
  Expected Output: Session variable email unset successfully.

Q9. Write a PHP script to destroy a session.  
  Input: Session contains variables.  
  Expected Output: Session destroyed successfully.

Q10. Write a PHP script to set a cookie named theme with the value dark that lasts for 7 days.  
   Input: None  
   Expected Output: Cookie theme set successfully for 7 days.

Q11. Write a PHP script to update the value of a cookie named language to French.  
   Input: Cookie language exists with the value English.  
   Expected Output: Cookie language updated to French.

Q12. Write a PHP script to list all cookies stored in the browser.  
   Input: Cookies user=John and theme=dark exist.  
   Expected Output:       
     user: John
     theme: dark

Q13. Write a PHP script to set a session variable named role to admin and retrieve its value.  
   Input: None  
   Expected Output:       
     Session variable role set to admin.
     Session value: admin

Q14. Write a PHP script to create a session that stores an array of items in a cart.  
   Input: Add item1 and item2 to the session.  
   Expected Output: Cart contains: item1, item2

Q15. Write a PHP script to extend the expiration time of a cookie named auth by 30 minutes.  
   Input: Cookie auth exists.  
   Expected Output: Cookie auth expiration extended by 30 minutes.

Q16. Write a PHP script to set a secure cookie named secure_token accessible only via HTTPS.  
   Input: None  
   Expected Output: Secure cookie secure_token set successfully.

Q17. Write a PHP script to check if a session has started.  
   Input: Session has not started.  
   Expected Output: Session is not active.

Q18. Write a PHP script to regenerate the session ID for security purposes.  
   Input: Session is active.  
   Expected Output: Session ID regenerated successfully.

Q19. Write a PHP script to store user preferences (color and font) in a session.  
   Input: Color: blue, Font: Arial.  
   Expected Output:       
     Preferences set.
     Color: blue
     Font: Arial

Q20. Write a PHP script to store the number of page visits in a session.  
   Input: User visits the page 3 times.  
   Expected Output: Page visited 3 times.

Q21. Write a PHP script to set a cookie with a path restriction to /admin.  
   Input: None  
   Expected Output: Cookie set for /admin path only.

Q22. Write a PHP script to set a cookie with a domain restriction to example.com.  
   Input: None  
   Expected Output: Cookie set for domain example.com.

Q23. Write a PHP script to retrieve the session ID.  
   Input: Session is active.  
   Expected Output: Session ID: <unique_id>

Q24. Write a PHP script to store the current timestamp in a session and display it.  
   Input: None  
   Expected Output: Session timestamp: 1700000000

Q25. Write a PHP script to delete all cookies set by the application.  
   Input: Cookies user, theme, and auth exist.  
   Expected Output: All cookies deleted successfully.

Q26. Write a PHP script to check if cookies are enabled in the user's browser.  
   Input: Browser allows cookies.  
   Expected Output: Cookies are enabled.

Q27. Write a PHP script to check if a session variable value is empty.  
   Input: Session variable cart exists but is empty.  
   Expected Output: Session variable cart is empty.

Q28. Write a PHP script to store multiple values in a session using an associative array.  
   Input: name=John, age=25, city=NY.  
   Expected Output:       
     Session data:
     name: John
     age: 25
     city: NY

Q29. Write a PHP script to set a session timeout of 15 minutes.  
   Input: None  
   Expected Output: Session timeout set to 15 minutes.

Q30. Write a PHP script to create a session and redirect the user to a welcome page.  
   Input: None  
   Expected Output: Session started. Redirecting to welcome.php.

Q31. Write a PHP script to store user login status in a session.  
   Input: User logs in.  
   Expected Output: User logged in. Session status: active.

Q32. Write a PHP script to clear all session data.  
   Input: Session contains variables.  
   Expected Output: All session data cleared.

Q33. Write a PHP script to store and retrieve the user's IP address in a session.  
   Input: User's IP address: 192.168.1.1.  
   Expected Output: Session IP address: 192.168.1.1

Q34. Write a PHP script to set a session variable that expires after the browser is closed.  
   Input: None  
   Expected Output: Session variable set to expire with browser.

Q35. Write a PHP script to log out a user by destroying their session.  
   Input: User is logged in.  
   Expected Output: User logged out. Session destroyed.

Q36. Write a PHP script to set a cookie with HTTPOnly flag enabled.  
   Input: None  
   Expected Output: Cookie set with HTTPOnly flag.

Q37. Write a PHP script to set a session variable that tracks user activity (e.g., pages visited).  
   Input: User visits home, about, and contact.  
   Expected Output: Pages visited: home, about, contact

Q38. Write a PHP script to display all session variables.  
   Input: Session contains user=John and email=john@example.com.  
   Expected Output:       
     user: John
     email: john@example.com

Q39. Write a PHP script to set a cookie that expires in the past to ensure deletion.  
   Input: None  
   Expected Output: Cookie expired and deleted.

Q40. Write a PHP script to start a session only if it is not already active.  
   Input: Session is not active.  
   Expected Output: Session started.

Q41. Write a PHP script to set a session variable and check if it is set.  
   Input: Set language to PHP.  
   Expected Output: Session variable language is set.

Q42. Write a PHP script to store user authentication token in a cookie.  
   Input: Token: 1234567890.  
   Expected Output: Authentication token stored in cookie.

Q43. Write a PHP script to set a cookie with a custom expiration date.  
   Input: Expiration date: 31-Dec-2024.  
   Expected Output: Cookie set to expire on 31-Dec-2024.

Q44. Write a PHP script to display a welcome message using a session variable.  
   Input: Session name=Alice.  
   Expected Output: Welcome, Alice!

Q45. Write a PHP script to delete a specific session variable.  
   Input: Delete session variable role.  
   Expected Output: Session variable role deleted.

Q46. Write a PHP script to set a cookie that is accessible only via JavaScript.  
   Input: None  
   Expected Output: Cookie accessible via JavaScript set.

Q47. Write a PHP script to prevent session fixation by regenerating the session ID after login.  
   Input: User logs in.  
   Expected Output: Session ID regenerated after login.

Q48. Write a PHP script to save session data to a file instead of using the default storage.  
   Input: None  
   Expected Output: Session data saved to file.

Q49. Write a PHP script to display the lifetime of the current session cookie.  
   Input: Default lifetime: 1440 seconds.  
   Expected Output: Session cookie lifetime: 1440 seconds.

Q50. Write a PHP script to redirect users to a login page if a session variable loggedIn is not set.  
   Input: Session variable loggedIn is not set.  
   Expected Output: Redirecting to login.php.

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!