Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Cookies in PHP

In this article will discuss about php cookies which let you save some value in your web browser.

Today, we are going to learn about php cookies which let you save some value like username etc while user is surfing your webpage.

Php has support for Http cookies. Cookies can be used to store some user information for your site in the user’s browser. This is helpful because when the same user again visits your site then browser sends the request along with the cookies. Your site may use these cookies to retrieve the user preferences for that particular user .So, it work just like your mini identity card which remember your details and present it when required. Cookies are saved in client local computer so that when user again visit your site then browser can identify the relevant cookies and perform accordingly.

Now we will learn how to create and retrieve cookie values with PHP.

setcookie

This method is used to store cookie value, the syntax for this method is : setcookie(name, value, expire, path, domain) and we have 5 arguments in which:

  • Name : For setting the name of cookie
  • Value : For setting the value for this cookie
  • expire: For setting the expire date for this cookie .The cookie will not work after expire date has passed
  • path : It is used to define the path on server where cookie will be stored.
  • domain : The name of domain
  • An example is :

    Listing 1: PHP Setting Cookies example

    <html>
    <head>
    	<title>MrBool Development Tutorial</title>
    </head>
    <body>
    	<?php
    	//This is setting the expire variable to 30 days from today so that cookie will be valid for 30 days from today
    	$expire=time()+60*60*24*30;
    	//Here we are setting the cookie using the setcookie method
    	setcookie("user", "Anurag", $expire, “www.mysite.com”);
    	?>
    </body>
    </html>
    

    We have set the name of cookie to be user .The value of this cookie is set to be Anurag. We set the expiry date for this cookie which is set 30 days from today. Since we don’t give any path so cookie will be placed in default path.

    Retrieve cookie values

    Listing 2: PHP Retrieve Cookies example

    <html>
    <head>
    	<title>MrBool Development Tutorial</title>
    </head>
    <body>
    	<?php
    	//Here we check whether the cookie named user is set or not
    		if (isset($_COOKIE["user"])){
    		//If cookie is set we show welcome message.
    			echo "Welcome to our site : " . $_COOKIE["user"] . "!<br />";
    		}else{
    		//if cookie is not set we show error message
    			echo "Welcome guest, Kindly register yourself as a user!<br />";
    		}
    
    	?>
    
    </body>
    </html>
    

    The isset function is used to check whether there is a cookie with name given or not. Like here isset function check whether there is a cookie with name “user”. We retrieve the value of the cookie by writing $_COOKIE[“user”]. This will retrieve the cookie value and show that on the screen.

    Hence in this example first we check if the cookie is present or not. If it is there then we show a welcome message otherwise ask the user to register himself/herself.

    Deleting Cookie value

    If you want to delete the cookie then you have to again use the setcookie function. In php there is no direct method to delete a cookie .Instead we just set the expire date in setcookie as negative so that cookie gets deleted instantly. Like we have given an example below

    Listing 3: PHP Delete Cookies example

    <html>
    <head>
    	<title>MrBool Development Tutorial</title>
    </head>
    <body>
    	<?php
    		//setting cookie expiry to past so that it gets deleted
    		setcookie("user", "", time()-3600);
    	?>
    </body>
    </html>
    

    Here we have set the cookie expiry value to be a value which is earlier than present time i.e. time()-3600.Since this is in past so browser will delete the cookie and task is accomplished easily.We have given the cookie value as blank but delete is not dependent on that.Main thing is that you always give correct cookie name to delete and set time to some time in past as done in above Listing 3 example

    Advantage

    They will not take any server resource since they are stored in user local browser. Hence it save a lot of server resource. You can store user local preferences like you may store the background color or image they like. So that when they next visit your site then your site will read the cookies and change the background accordingly.

    You may use it in sites like shopping cart(But beware cookie editing may cause trouble to your site), you can use it store user information.

    They are really easy to implement as you saw in this article. Cookie can be configured to expire when they are not required

    Problems in making use of cookies

    There are several problems also in making use of cookies:

    • Modern browser have feature to edit cookie. This may be dangerous since attacker may make changes in cookie file
    • Cookies can be viewed so better it must be encrypted.
    • If the cookies are turned off then a major portion of your website functionality dependent on cookie will not work. To prevent yourself from this situation you might need to have a check whether the user has turned the browser cookie to on/off and accordingly display a warning message.

    Note :

    • Always use the cookies to store general(non risky) user preferences so that if any malicious user even tamper with the cookie then too your website don’t get affected.
    • Server can only access cookie value from your browser and none of your personal info.
    • It's not possible to execute any code with php so cookie cannot infect your computer with any virus.

    So, today you learned everything about php cookies. If you still have any doubts you may always place your questions in the comments and I will answer them as soon as possible. And yeah please let me know if you want article on any particular topic so that I will prepare the same. See you next time with more informative article.



    My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

    What did you think of this post?
    Services
    [Close]
    To have full access to this post (or download the associated files) you must have MrBool Credits.

      See the prices for this post in Mr.Bool Credits System below:

    Individually – in this case the price for this post is US$ 0,00 (Buy it now)
    in this case you will buy only this video by paying the full price with no discount.

    Package of 10 credits - in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

    Package of 50 credits – in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


    > More info about MrBool Credits
    [Close]
    You must be logged to download.

    Click here to login