Vous êtes sur la page 1sur 12

Sessions and cookies

When you are working with an application, you open it, do


some changes and then you close it. This is much like a
Session.
The computer knows who you are and when you start the
application and when you end.
But on the internet there is one problem: the web server does
not know who you are and what you do because the HTTP
address doesn't maintain (keep) state.

Session (, -. )

Where the user information is stored


on the server (in a file or in a database) Session Variables
on user computer - Cookies Variables

Sessions
Session - Variables are kept on a server in files. Path of the file is defined in the php.ini file.
session.save_path = "C:/TEMP"
Example:
page1.php
<?php

session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

// Starting a PHP Session.


// It must appear BEFORE the <html> tag
// Storing a Session Variable

?>

page2.php
<?php

session_start();
echo $_SESSION['favcolor'] . "<br />";
// green
echo $_SESSION['animal'] . "<br />";
// cat
echo date('Y m d H:i:s', $_SESSION['time']) . "<br />";
?>

Sessions file name (on the server):


C:/TEMP/sess_da930a91978813156affbaf9764a7615
Sessions file content:
favcolor|s:5:"green";animal|s:3:"cat";time|i:1204887433;";

Destroying a Session
If you wish to delete some session data, you can use the unset()
or the session_destroy() function.
<?php

session_start();
session_unset();

// Free all session variables

// or unset($_SESSION['favcolor']);
?>
<?php

session_start();
// You can also completely destroy the session by calling:
session_destroy();
// it will reset your session and
// you will lose all your stored session data.
// or $_SESSION = array();
?>

Cookies
Variables are kept on the user computer

Create a Cookie
setcookie (name, value, expire, path, domain);
// This function must appear BEFORE the <html> tag.
name value expire path -

The name of the cookie.


The value of the cookie.
The time the cookie expires.
The path on the server in which the cookie will be available on.
The default value is the current directory that the cookie is being set in.
domain - The domain that the cookie is available.

Example:
- Create a Cookie
// We will create a cookie named "user" and assign the value "Alex Porter" to it.
// And the cookie should expire after one hour.
setcookie("user", "Alex Porter", time()+3600);
- Print an individual cookie
echo $_COOKIE["user"];
// or
echo $HTTP_COOKIE_VARS["user"];
- Delete a Cookie
setcookie("user", "");

Cookies file name for IE:


mmelkony@localhost[2].txt
Cookies file content for IE:
user
Alex+Porter
localhost/aaa/session/Cookie/
1024
2652908544
29917836
1022516912
29917828
*

header
void header ( string string [, bool replace]);

array headers_list (void);

<?php
// HTTP/1.1

header ("Content-type: image/jpeg");


header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

?>
<?php
header("Location: http://www.example.com/"); // Redirect browser
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf for download
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in ex.pdf
//readfile('ex.pdf');
?>

no cache
<?php
// Date in the past
header("Expires: Mon, 23 May 1995 02:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
?>

Vous aimerez peut-être aussi