0% found this document useful (0 votes)
6 views5 pages

PHP Cookies Sessions

The document explains how to use cookies and sessions in PHP for user identification and data storage. It covers creating, modifying, and deleting cookies, as well as starting sessions, setting session variables, and destroying sessions. Additionally, it includes a simple example of a form for adding two numbers using PHP.

Uploaded by

dy28175
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

PHP Cookies Sessions

The document explains how to use cookies and sessions in PHP for user identification and data storage. It covers creating, modifying, and deleting cookies, as well as starting sessions, setting session variables, and destroying sessions. Additionally, it includes a simple example of a form for adding two numbers using PHP.

Uploaded by

dy28175
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP Cookies

A cookie is often used to identify a user. A cookie is a small file that the
server embeds on the user's computer. Each time the same computer
requests a page with a browser, it will send the cookie too. With PHP, you
can both create and retrieve cookie values.

Create and Retrieve a Cookie:


<?ph p
$cookie_name = "user";
$cookie_vaIue = "John Doe";
setcookie($cookie_name, $cookie_vaIue, time() + (86400 * 30), "/"); //
86400 = 1 day

< htmI>
< body>

<?ph p
i !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>
</htmI>

Modify a Cookie Value


<?ph p
$cookie_name = "user";
$cookie_vaIue = "Alex Porter";
setcookie($cookie_name, $cookie_vaIue, time() + (86400 * 30), "/");

< htmI>
< body>

<?php
i !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>
</htmI>

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

< htmI>
< body>

<?php
echo "Cookie ’user’ is deleted.";
</body>
</htmI>

PHP Sessions
A session is a way to store information (in variables) to be used across
multiple pages.

Unlike a cookie, the information is not stored on the users computer.

When you work 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.
It knows 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 or
what you do, because the HTTP address doesn't maintain state.
Session variables solve this problem by storing user information to be used
across multiple pages (e.g. username, favorite color, etc). By default,
session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are
available to all pages in one application.
Start a PHP Session
A session is started with the session_start() function.

Session variables are set with the PHP global variable: $ SESSION.

<?php
// Start the session
session start();

< ! DOCTYPE htmI>


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

</body>
</htmI>

Modify a PHP Session Variable


To change a session variable, just overwrite it:
<?ph p
session_start();

< !DOCTYPE htmI>


<htmI>
<body>

<?php
// to change a session variable, just overwrite it
$ SESSION["favcolor"] = "yellow";
print_r($ SESSION);

</body>
</htmI>
Destroy a PHP Session
To remove all global session variables and destroy the session,
use session unset() and session destroy():
<?ph p
session_start();
< !DOCTYPE htmI>
<htmI>
<body>

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

// destroy the session


session destroy();
</body>
</htmI>

Adding 2 numbers using PHP

<htmI>
<head>
<titIe> Forms Handling in PHP</titIe>
</head>
<body>
<h1>Addition of two numbers: </h 1>
<form action="form_process.php" method="POST">
Enter Number 1: <input type="text" name="num 1"
id="num1"/> <br>
Enter Number 2: <input type="text" name="num2"
id="num2"/> <br> <br>
<button type="submit">ADD</button>
</form>
</body>
</html >

<?php
$n1 = $_POST['num 1'];

$n2 = $_POST[’num2’];
$sum = $n1 +$n2;

echo "<script>aIert(’sum = + $sum+’!’); </scripts";


include ’myform.html",

You might also like