Web development tips (sessions)

 

"Always remember to unset your sessions after creating them. Too many unresolved sessions could lead to unplanned system behaviour."(Mark Anthony Graham)

A bit about sessions

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

Sessions in PHP sample code

//php add/set session syntax //start a session session_start(); //store a value in a session $_SESSION['name'] = 'Mark'; //retrieve a value from a session echo $_SESSION['name']; //outputs 'Mark'

//unset session in php syntax //unset a single session variable unset($_SESSION['Mark']);
//destroy the session session_destroy();

Sessions in Python sample code


#In Python, the syntax for creating a session is as follows: # Import the necessary modules import requests from requests.auth import HTTPBasicAuth # Create the session session = requests.Session() # Authenticate the session session.auth = HTTPBasicAuth('username', 'password') # Make a request using the session response = session.get('http://example.com/')

#To unset a session in Python, you can use the following syntax: # Close the session session.close()

Conclusion
In some unique cases you may have to keep the sessions unclosed. But even with that do well to have in mind the function of each session before it becomes confusing.

Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

Building a Fortified Database Connection in PHP

Mastering Error Handling in PHP: A Deep Dive into `@` and `try...catch`