session_regenerate_id

This function replaces the current session id with a newly generated one .

Syntax: session_regenerate_id()

Description:

session_regenerate_id() will replace the current session id with a new one, and keep the current session information.

Example:
Code:

session_start();

$old_sessionid = session_id();

session_regenerate_id();

$new_sessionid   session_id();

echo  "Old Session: $old_sessionid
"; echo "New Session: $new_sessionid
";

Explanation:

Delete a Cookie

By default, the cookies are set to be deleted when the browser is closed. We can override that default by setting a time for the cookie’s expiration but there may be occasions when you need to delete a cookie before the user closes his browser, and before its expiration time arrives. To do so, you should assure that the expiration date is in the past. To destroy the cookie, simply use setcookie.

Example:
Code:

// set the expiration date   to one hour  ago
setcookie("lastVisit",   "date("G:i - m/d/y")", time()-3600);

Explanation:

It sets expiration time 1 hour ago.

$_COOKIE()

The PHP $_COOKIE variable is used to retrieve a cookie value.

Syntax: $_COOKIE[‘cookie name’]

Description:

The parameter “cookie name” is Required.

Example:
Code:

if(isset($_COOKIE['lastVisit']))
   { 
 $last = $_COOKIE['lastVisit']; 
 echo "You last visited on ". $last; 
 } 
 else 
 { 
 echo "Welcome to our site!"; 
 } 

Explanation:

This code first checks if the cookie exists. If it does, it tells them when they last visited. If they are new, it skips this and prints a welcome message.

setcookie()

This function is used to set a cookie.

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Syntax: setcookie(name, value, expire, path, domain,secure,httponly).

Description:

When you create a cookie,using the function setcookie,the parameters name, value, expire, path, domain are used.All the parameters except the name argument are optional.

The first parameter “name” is Required. It specifies the name of your cookie. You will use this name to later retrieve your cookie, so don’t forget it!

The second parameter “value” is Optional. It specifies the value that is stored in your cookie. Common values are username(string) and last visit(date).

The third parameter “expiration” is Optional The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

The fourth parameter “path” is Optional. It specifies on the server in which the cookie will be available on. If set to ‘/’, the cookie will be available within the entire domain. If set to ‘/example/’, the cookie will only be available within the /example/ directory and all sub-directories such as /example/examp/ of domain. The default value is the current directory that the cookie is being set in.
domain.

The fifth parameter “domain” is Optional.It specifies that the cookie is available to. To make the cookie available on all subdomains of example.com (including example.com itself) then you’d set it to ‘.example.com’. Although some browsers will accept cookies without the initial ., ยป RFC 2109 requires it to be included. Setting the domain to ‘www.example.com’ or ‘.www.example.com’ will make the cookie only available in the www subdomain.

The sixth parameter “secure” is Optional.It specifies that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it’s on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER[“HTTPS”]).

The seventh parameter “httponly” is Optional.It specifies When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won’t be accessible by scripting languages, such as JavaScript. This setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers). Added in PHP 5.2.0. TRUE or FALSE.

Example:
Code:

//Calculate 30 days in the future
//seconds * minutes * hours * days + current time
$inOneMonth = 60 * 60 * 24 * 30 + time(); 
setcookie('lastVisit', date("G:i - m/d/y"), $inOneMonth); 

Explanation:

In the above example we have created a cookie that stores the user’s last visit. We want to ignore people that take longer than one month to return to the site, so we have set the cookie’s expiration date to one month in the future!

session_unregister()

This function Unregister a global variable from the current session.

Syntax: session_unregister ( string name )

Description:

The parameter “string name” is specified to be unregistered.

This function unregisters a session variable registered with session_register(). If $_SESSION was used to register the session variable, use unset() to unregister it.

Example:
Code:

 
session_start();
session_register("session_variable");
$session_variable   = "xyz123";
print "session_variable: " . $session_variable;
session_unregister("session_variable");
Page 1 of 3123
eXTReMe Tracker