Popular Posts

Friday, 2 July 2010

session cookies header

For orignial post kindly visit www.apepoint.com

for the original contents kindly visit www.apepoint.com

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site A visitor accessing your web site is assigned a unique id, the so-called session id.

A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, e-mail id , etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage

starting a php session

Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent.

PHP Code:
session_start();
?>

Creating and reading session variables

Variables can be assigned to a session by using the $_SESSION global variable. Any value that is assigned to a $_SESSION variableis readable by any script that is served by your web server and is viewed in the same session.

session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

Cleaning and destroying your session.

some time we want to clear a session data so we can use the below function

unset($_SESSION['userid']);

You can also completely destroy the session entirely by calling the session_destroy function.

PHP Code:

session_start();
session_destroy();
?>

HTTP Headers

There are several things importance of HTTP header but generally in php we use in re-direct and in graphics below is an example of a redirect code

header("Location:home.php");

the script will redirect to home.php page


Cookies

PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the setcookie() or setrawcookie() function. Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser setcookie — Send a cookie

php start
$value = 'something from somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
php close


php start
// Print an individual cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];
// Another way to debug/test is to view all cookies
print_r($_COOKIE);
php close


Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C". If you wish to assign multiple values to a single cookie, just add [] to the cookie name.

No comments:

Post a Comment