PHP Session and PHP Cookies

PHP Session and PHP Cookies

The php session and php coockies are very mendatory portion to develop a php project. The session and cookies both are used to store informsation and use that information throughout all pages or functions.

What is PHP Session?

A php session is a process to store data in server, for a local application or desktop appliction you use to open the application and use it and finally cloase it. But while you are using a sercer side program the internet, the server does not remember the data you used in previous page so to solve that we need to use session. A php session use to store saary type data. You can store an array also inside a session.

When you have to use a session you must start the session at the very begining of the page. else the session will create problem to use it. the syntex is session_start();

And to insert a velue inside a session is $_SESSION and then the key inside a array sign, the syntex is $_SESSION[‘name’] = ‘the name value here’; thus a value will be sassigned inside a session veriable.

To use a session just call the session with its array key and you can be able to assign the value inside a veriable or direct can be able to print the value. and the syntex is $name = $_SESSION[‘name’]; or echo $_SESSION[‘name’]

Now the example to start a session and assign a value inside a session and store the value in a veriable or print that.

suppose a php page session_value_assign.php to set the session values in php.

Example:


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

<?php
// Set session variables
$_SESSION["first_name"] = "First Name Here";
$_SESSION["last_name"] = "Last Name Here";

// or to store an array in a session veriable

$_SESSION['name'] = array('first_name'=>'First Name Here', 'last_name'=>'Last Name Here');
?>

</body>
</html>

suppose a php page session_value_get.php to get the session values and print in php.

Example:


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

<?php
// Print session variables
echo $_SESSION["first_name"];
echo " ";
echo $_SESSION["last_name"];

//it will print First Name Here Last Name Here

// the way to print all the session or a array inside a session veriable.

print_r($_SESSION); // it will print all the session
//or print array inside a particular key
print_r($_SESSION['name']); // it will print the array inside of a session veriable.

?>

</body>
</html>

How to rewrite a PHP Session Veriable,

to rewrite a session veriable just reassign a value to the session with its key nad the new value will be assigned to the session array of that particuler key.

How to Destroy a PHP Session?

To destroy a global php session there is 2 wau you can unset the session or destroy the sission, and the syntex to unset and destroy session are session_unset(); and session_destroy();

The example of Destroy a PHP Session cose is given below

Example:


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

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

What is a php Coockies.

A php coockies is used to identify the the user and the data is stored in clients machine or more proper is in clients browser by the server. Each time the a computer requests a page through a browser, the server will send the cookie also. Using php we can set and get coockies .

How to start a Cooxckies?

to create a cookie the syntex is setcookie(name, value, expire, path, domain, secure, httponly); and it will created and stores on users browser.

the global veriable of coockie is $_COOKIE, now an example of start and creation of coockie is gine below,

Example:


<?php
$cookie_name = "coockie_name";
$cookie_value = "The value of the coockie";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

Ho to get the coockie?

to get the coockie first check if the coockie is available and then use $_COOCKIE and then coockie name inside array sign, an example is given below on hoe to get coockie

Example:


<html>
<body>

<?php
if(isset($_COOKIE['coockie_name'])) {
echo $_COOKIE['coockie_name'];
}
?>

</body>
</html>

How to Modify a Cookie Value?

to modify a cookie and reassign value on it use the function setcookie(), as it will reassign the value expiry time etc.

Example:


<?php
$cookie_name = "coockie_name";
$cookie_value = "The value of the coockie";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

How to Delete a Cookie?

to delete a coockie also use setcookie() function and set the expiry time in minus thus browser will remove the coockie, an example is given below.

Example:


<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>

I hope the post PHP Session and PHP Cookies helped you,

Thanks for Reading

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.