PHP Session and PHP Cookies

PHP Session and PHP Cookies

The PHP session and PHP cookies are a very mandatory portion to develop a PHP project. The session and cookies both are used to store information and use that information throughout all pages or functions.

What is the PHP Session?

A PHP session is a process to store data in the server, for a local application or desktop application you use to open the application and use it and finally close it. But while you are using a server-side program the internet, the server does not remember the data you used in the previous page, so to solve that, we need to use the session. A PHP session use to store array 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 beginning of the page. else the session will create a problem to use it. the system is session_start();

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

To use a session just call the session with its array key and you can be able to assign the value inside a variable or direct can be able to print the value. and the system 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 variable 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 Variable,

to rewrite a session variable just reassign a value to the session with its key and the new value will be assigned to the session array of that particular key.

How to Destroy a PHP Session?

To destroy a global PHP session there is 2 way you can unset the session or destroy the session, and the syntax to unset and destroy session are session_unset(), and session_destroy();

The example of Destroy a PHP Session code 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 are PHP Cookies?

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

How to start a Cookies?

to create a cookie the syntax is set the cookie(name, value, expire, path, domain, secure, httponly); and it will create and store on the user’s browsers.

the global variable of a cookie is $_COOKIE, now an example of start and creation of cookie is given 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 cookies?

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

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 cookie also use setcookie() function and set the expiry time in minus thus browser will remove the cookie, 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.