Session is useful to store user information on the server for later use (i.e. username, shopping items, etc) .
However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
Set the Session
<?php session_start(); ?> <html> <head> <title></title> </head> <body> </body> </html>
Storing the Session variable
<?php session_start(); $_SESSION['user'] = 'tanin'; ?> <html> <head> <title></title> </head> <body> <?php echo "User name = ". $_SESSION['user']; ?> </body> </html>
Session example
<?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "This page visit". $_SESSION['views']." times"; ?>
Session Id
<?php echo session_id(); ?>
Session Name
<?php session_start(); $_SESSION['user'] = 'tanin'; // store session data echo "Pageviews = ". $_SESSION['user']; //retrieve data echo "Session name = ".session_name("user"); ?>
Session save path
<?php echo session_save_path(); session_save_path('session/'); ini_set('session.gc_probability', 1); echo session_save_path(); ?>
Unset the Session
<?php unset($_SESSION['views']); ?>
Set the Session
<?php session_destroy(); ?>
What is php Cookie
A cookie is a text-only string that takes a place in the memory of user’s browser. If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.
What is php session
Session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.
The Differences between php cookie and php sessions
We got three differences in general.
1. The key difference would be cookies are stored in client side and sessions are stored in server side.
2. The second difference would be cookies can only store strings. We can store our objects in sessions. Storing objects in sessions were really useful according to my experience.
3. Another difference was that we could be save cookie for future reference, but session couldn’t. When users close their browser, they also lost the session.
No comments:
Post a Comment