Vous êtes sur la page 1sur 15

PHP Session and Cookies

Presented By : Akshay Vilas Farakate


Contents
1. What is php session ?
2. Need of php session.
3. Session functions.
4. Create session.
5. Get php session variable value.
6. Modify a php session variable.
7. Destroy a php session.
8. PHP cookies.
9. Create cookies.
10. Modify a cookie value.
11. Delete a cookie.
12. Check if cookie are Enabled.
PHP Session
What is the PHP session ?

• It is the user information in the Server side

• Work by creating a unique id(UID) of user

• UID is either stored in a cookie or is propagated in the


URL
Why should we use Session ?
 Need for data to stored on the server
 Unique session information for each user
 Transient data,only relevant for short time
 Data does not contain secret information
 Similar to Cookies,but it is stored on the server
 More secure,once established,no data is sent back and forth between
the machines
 Works even if cookies are disabled
Session Functions
 session_start()
 session_destroy()
 session_unset()
 session_name()
 session_register()
 session_id()
 session_regenerate_id()
 session_is_registered()
 session_unregistered()
PHP Session
 Starting a PHP session:
<?php
session_start();
?>
 This tells PHP that a session is requested
 A session ID is then allocated at server end
 Session ID looks like a very large random number
e.g.sess_f123478132479705385869262707asjkhfa7891234g
Create a session
$_SESSION[‘session_name’] :Global variable used to store user data in a session.

• Sample program: Output:


<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>
Get PHP Session Variable Values
we create another page called "session2.php". From this page, we will
access the session information we set on the first page ("session1.php").

 Example:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

Output :
Modify a PHP Session Variable
 To change a session variable, just overwrite it:
Example:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>
Output:
Destroy a PHP Session
 To remove all global session variables and destroy the session,
use session_unset() and session_destroy():
Example
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>
Output:
PHP Cookies
 Used to identify a user

 It is a small file that the server embeds on the user's


computer

 We can both create and retrieve cookie values


Create Cookies
A cookie is created with the setcookie() function
 Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
Example:
 <?php
$cookie_name = "user";
$cookie_value = “Akshay";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html> Output:
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
Modify a Cookie Value
To modify a cookie, just set (again) the cookie using the setcookie() function:
Example
 <?php
$cookie_name = "user";
$cookie_value = "Rushikesh";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
} Output:
?>

</body>
</html>
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in
the past:
Example
 <?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>
 Output:
Check if Cookies are Enabled
 The following example creates a small script that checks whether cookies are enabled
Example :
 <?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?> Output:

</body>
</html>