Working with Sessions in PHP


/ Published in: PHP
Save to your folder(s)

As HTTP is state protocol.To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID.Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data,if session id is not present on server PHP creates a new session, and generate a new session ID.


Copy this code and paste it in your HTML
  1. <?php
  2. //starting a session
  3. //Creating a session
  4. $_SESSION['user_info']=['user_id'=>1,'first_name'=>'php','last_name'=>'scots','status'=>'active'];
  5.  
  6. //checking session
  7. if(isset($_SESSION['user_info'])){
  8. echo "logged In";
  9. }
  10.  
  11. //un setting remove a value from session
  12. unset($_SESSION['user_info']['first_name']);
  13.  
  14. // destroying complete session
  15.  
  16. ?>

URL: https://www.onlineinterviewquestions.com/core-php-interview-questions/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.