Cookies

Cookie is an item of data that a web server saves to your computer’s hard disk via a web browser. It can contain almost any alphanumeric information and can be retrieved from your computer and return to the server. Common uses include session tracking, maintaining data across multiple visits, holding shopping cart contents, storing login details, and more. Because of this privacy implications, cookies can be read-only from the issuing domain. In other words, if a cookie is issued by, for example,oreilly.com, it can be retrieved only by a web server using that domain. This prevents other websites from gaining access to details for which they are not authorized. Because of the way the internet works, multiple domains, each of which can issue its own cookies. When this happens, they are referred to as third-party cookies. Most commonly, these are created by advertising companies in order to track users across multiple websites. Because of this most browsers allow users to turn cookies off either for the current server’s domain, third-party servers, or both. Fortunately, most people who disable cookies do so only for third-party websites.

Setting a Cookie:

Setting a Cookie in PHP is a simple matter. As long as no HTML has yet been transferred, you can call the setcookie function, which has the following syntax.

setcookie(name,value,expire,path,domain,secure,HTTP only);

Cookies in Php
Cookies in Php

Accessing a Cookie:

Reading the value of a cookie is as simple as accessing the $_COOKIE system array. For example, if you wish to see whether the current browser has the cookie called user name already stored and if so, to read its value, use the following:

if(isset($_COOKIE[‘username’])) $username=$_COOKIE[‘username’];

HTTP Authentication:

HTTP authentication uses the web server to manage users and passwords for the application. It’s adequate for most applications that ask the user to login in, although some applications have specialized needs or more stringent security requirements that call for other techniques.

Use the following code to make this happen:

<?php
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
echo"Welcome User".$_SERVER['PHP_AUTH_USER'].
    "Password:".$_SERVER['PHP_AUTH_PW'];
}
else
{
header('WWW-Authenticate:Basic realm="Restricted Section"');
header('HTTP/1.0 401 Unauthorized');
die("Please enter your username and password");
}
?>
Translate »