Creating Cookies with php programming Language. You may be seen cookies may be in your browser setting. I am Sure every one know What is Cookies are but how we actually use them and how useful are there in every day programming on websites. A cookies is a piece of data or a file that stored specific information and its unique to you and the website that you viewing. So, If you view on your browser a website that set a specific cookies and you access to that website depending on the expiration time cookies that piece of data will be relate. That will be relate to your computer to the website. So, This way website can store data about users or about your specific preferences on in there website and it can be stored on your computer for later access.
How we can go about setting cookies and why they useful now there are some of the reason why they are useful for is just storing data that's need to be used to be later day for long expression time. You may already seen my article setting session in php. Cookies again allow you to set a much longer expression date. So, sessions are close as soon as your is close in the connection is short in the server. However with cookies we could store cookies for year.
Code for Creating Cookies With php
Set.php (Screenshot1)
|
set.php Screenshot1 |
<?php
setcookie('username','alex','time()+10')
?>
View.php (Screenshot2)
|
Screenshot2 |
<?php
echo $_COOKIE['username'];
?>
Output
|
Screenshot3 |
|
Screenshot4 |
|
Screenshot5 |
To set cookies we use setcookie function takes few arguments. Here i will show you few arguments. I will go through three main arguments and three main arguments we look at first one is the cookies name you are setting then the value of cookies want to store in the cookies and the expression date. The time in which the cookies going to be valid for. Now remember when we look at sessions we write like $_SESSION['name']='value'; and in Cookies we write like setcookie('name','value','time');. Here in this example i had set cookies in set.php with setcookie function with arguments name is username value is alex and the expression time is something that how much time we want to put for the cookie exit to the browser. Its value of time expression is in seconds. For set the time expression argument we need to use timestamps infact in every php when we need to use date time function we need timestamps to retrieve date time etc.
If you don't know what is timestamps please read articles of Extra Tutorials on timestamps. Timestamps in php is basically around of seconds current day in seconds. So, We use time function to grap a timestamps. So, Here we had time() which will retrieve current time then we add more 10 seconds to delay the cookies.
Now in view.php i had echo out the viewing the cookies and you can see after 10 seconds cookies will be expired. After refresh output you can see how cookies had been expired. Because timestamps gone less then the current time.
So, You can Use cookies in login system where you want to your user can access logged time interval after that you want to logout the user. So, How basically cookies are useful but remember cookies are stored on users computer. So, There are dangers to use things like sensitive data that you don't want to user to change for example you had a shopping card and you have total set in cookies. You don't necessary want this unless you using some form of encryption some database used to store that information can be stored to the user computer and that can harm websites.
Code for Unset Cookies with php
Set.php
<?php
setcookie('username','alex','time()-10')
?>
View.php (Screenshot2)
|
Screenshot |
|
Screenshot2 |
Now For Unsetting the cookies there is not any function for unset or make sure cookies file must delete from the users computer is to subtract the seconds of the cookies. Now If you will set the cookies it will not set because it is unset. Now why you need to unset the cookies or what is need to unset the cookies we answers quite simple login system if you log you user storing file where they logged in cookies and you may be log in for 1000 seconds if you want to log them out you may have logout button now when i click logout button we need some way insuring that cookies is deleted and unset so, users no longer logged in so obviously clicking logout button we perform some kind of operation like subtrating seconds from timestamps to ansure that the cookies had unset and the users no longer logged in. So, thats the simple example for unset the cookies and that basically simple way.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..