Setting Cookies in PHP

Setting Cookies in PHP

Setting Cookies in PHP

Setting Cookies in PHP. This is a normal practice which lots of web master do to track their USER or Audience visits & interests. Many Large sites has cookies to gather data and analytics from users , in order to study the behaviour of their User and customers.The Ultimate goal is very obvious , is to increase sales or generating more leads from their marketing effort.

As a Responsible web master, the good practise is to let your User or Audience to choose, whether to accept the cookies.

Cookies can be Harmful at times . There are malicious site which generate cookies to track the User info. Such as Credit Card Number , personal information, IP Protected information. The hijack information will be sell to third party in exchange for financial gains.

In this article we will step into how to embed cookies into our Website .

How to Set Cookies

Setting Cookies in PHP are not difficult , follow the steps below and you will get there.

  1. use the Set Cookie Function . setcookie() ; Check out here to learn how to set the parameter inside the Function.
  2. Set how much the tracking time for the cookie will expire.
  3. Pass a name to your cookies.

How the Scripts  below works

  1. The Script will check the Cookie Name “User” is it available
  2. If  Yes , it will grab the “User” Information and echo out a random number.
  3. If No , there is no user, it will echo  ” No User”

Check out the Code Below

 

<?php

if(isset($_COOKIE["user"])){
	
	echo $_COOKIE["user"];
}
else
{
	echo"No Cookies";
	
}
$trackingtime = 86400; // equal to 1 day
$randomNumber = rand(0,1000);
setcookie("user","Web".$randomNumber,time()+$trackingtime);


?>



 

Learn how to include other PHPinside your PHP Script .Here

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − six =