डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

PHP cookie session

PHP cookie

A cookie is used to store user information on the user's computer.

Create a cookie

setcookie(name, value, expire, path)

example

setcookie("username","capscom",time()+ (86400*60), "/");

where 
username: cookie name
capscom: cookie value
time()+(86400*60): time of 2 month for this time a cookie will store in user computer, 86400 seconds = 1 day, time() is current time.
/ - a path for whole website

Get a cookie value

To get a cookie a value we will use $_COOKIE super global variable.

Note: make sure cookie is set before getting a cookie value

Example

<?php
if( isset($_COOKIE['username']) ){
      echo $_COOKIE['username'];
}
else{
echo "Cookie was not set.";
}
?>

Modify a cookie

a cookie can be modified by replacing an existing cookie.

example


<?php
setcookie("username","capscom tehnology", time()+(86400*60), "/");
?>

Delete a cookie

To delete a cookie set cookie time in the past by subtracting seconds from time()

example


<?php
setcookie("username","", time()-(86400*60))
?>

Check cookie is accessible or not

count all existing cookies using the count() function if the counter value is greater than 0 it means there is a cookie enabled otherwise not.

example


<?php
if(count($_COOKIE) > 0 ){
 echo "cookie are accessible";
}
else{
echo "cookie are not accessible";
}
?>

Session

When a user visits any website a web page does not identify the user who is this and what he is doing on the web page, using sessions we can store user information and can track user activity by putting user information on the server.

Note:

Whenever you create, access, delete a session make sure the session_start() function is calling at top of every php page or code.

Create a session

$_SESSION['session_variable_name']= value;

example

$_SESSION['username']="CAPSCOM TECHNOLOGY";

Get a session

echo $_SESSION['username'];

Delete a session

// unset all session variables
session_unset();
// destroy all session variables
session_destroy();
© 2016 - 2023, All Rights are Reserved.