What are Session cookies
Session cookies, also known as 'temporary cookies', help websites recognise users and the information provided when they navigate through a website. Session cookies only retain information about a user's activities for as long as they are on the website. Once the web browser is closed, the cookies are deleted. These are commonly used on shopping websites or e-commerce websites.We have already talked about cookies, Learn all about cookies.Cookies are the reason Trednix.com remembers your username between visits and the reason you don’t necessarily need to log in to your Hotmail account every time you open your browser. Cookie data typically contains a short set of information regarding when you last accessed a site, an ID number, and, potentially, information about your visit.
Let’s now look at the basic syntax used to create a cookie.
<?php setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain], [secure], [httponly]); ?>HERE, Php“setcookie” is the PHP function used to create the cookie.
“cookie_name” is the name of the cookie that the server will use when retrieving its value from the $_COOKIE array variable. It’s mandatory.
“cookie_value” is the value of the cookie and its mandatory
“[expiry_time]” is optional; it can be used to set the expiry time for the cookie such as 1 hour. The time is set using the PHP time() functions plus or minus a number of seconds greater than 0 i.e. time() + 3600 for 1 hour.
“[cookie_path]” is optional; it can be used to set the cookie path on the server. The forward slash “/” means that the cookie will be made available on the entire domain. Sub directories limit the cookie access to the subdomain.
“[domain]” is optional, it can be used to define the cookie access hierarchy i.e. www.cookiedomain.com means entire domain while www.sub.cookiedomain.com limits the cookie access to www.sub.cookiedomain.com and its sub domains. Note it’s possible to have a subdomain of a subdomain as long as the total characters do not exceed 253 characters.
“[secure]” is optional, the default is false. It is used to determine whether the cookie is sent via https if it is set to true or http if it is set to false.
“[Httponly]” is optional. If it is set to true, then only client side scripting languages i.e. JavaScript cannot access them.
Let’s now look at an example that uses cookies.
We will create a basic program that allows us to store the user name in a cookie that expires after ten seconds.
The code below shows the implementation of the above example “cookies.php”.
<?php setcookie("user_name", "Nnamchi israel", time()+ 60,'/'); // expires after 60 seconds echo 'the cookie has been set for 60 seconds'; ?>
Why and when to use Sessions?
Incase you don't know we have been talking about normal cookies.Now let's go to the reasons to use session cookies- You want to store important information such as the user id more securely on the server where malicious users cannot temper with them.
- You want to pass values from one page to another.
- You want the alternative to cookies on browsers that do not support cookies.
- You want to store global variables in an efficient and more secure way compared to passing them in the URL
- You are developing an application such as a shopping cart that has to temporary store information with a capacity larger than 4KB.
Creating a Session
In order to create a session, you must first call the PHP session_start function and then store your values in the $_SESSION array variable.Let’s suppose we want to know the number of times that a page has been loaded, we can use a session to do that.
The code below shows how to create and retrieve values from sessions
<?php session_start(); //start the PHP_session function if(isset($_SESSION['page_count'])) { $_SESSION['page_count'] += 1; } else { $_SESSION['page_count'] = 1; } echo 'You are visitor number ' . $_SESSION['page_count']; ?>output:
You are visitor number 1A session can also be unset and destroyed when expired or no longer needed. If not unset/destroyed, then the session file and session data will remain on the server unless the file or database storage for the session is deleted.
< ?php // use both unset and destroy for compatibility // with all browsers and all versions of PHP session_unset(); session_destroy(); ?>
Comments
Post a Comment