Chapter No 4
Chapter No 4
2 mark
Roles
When a user accesses a PHP-powered web page, the PHP server processes
the embedded PHP scripts within the HTML content.
Answer:
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.
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
3. Define cookies and session
Answer:
Cookie : A cookie is a small text file that is saved on the user’s computer.
The maximum file size for a cookie is 4KB. It is also known as an HTTP
cookie, a web cookie, or an internet cookie. When a user first visits a
website, the site sends data packets to the user’s computer in the form of a
cookie.
The information stored in cookies is not safe since it is kept on the client-
side in a text format that anybody can see.
Session values are far more secure since they are saved in binary or
encrypted form
Answer:
The mail() function allows you to send emails directly from a script.
As soon as the mail function is called PHP will attempt to send the
email then it will return true if successful or false if it is failed.
Multiple recipients can be specified as the first argument to the
mail() function in a comma separated list.
4 Mark
1. Difference between GET method and POST method
Answer
2. Write a suitable example of Web Page having multiple forms.
3. Write a suitable example of form having multiple submit buttons.
4. . How to create, modify and delete cookies.
Answer:
1. Creating a Cookie:
To create a cookie, you use the setcookie() function. It takes at least two parameters:
the name of the cookie and its value. Optionally, you can include additional
parameters such as expiration time, path, domain, etc.
Code:
<?php
setcookie("username", "john_doe", time() + 3600, "/");
echo "Cookie 'username' created.";
?>
2.Modify a cookie
To modify a cookie, just set (again) the cookie using
the setcookie() function:
Code :
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
2. Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration
date in the past:
Code:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
Here cookies will be automatically expire when the expiry time will pass away
The PHP code in the example below simply starts a new session.
Example:
<?php
// Starting session
session_start();
?>
Get session
you can access session variables using the $_SESSION superglobal array. This array
holds key-value pairs of session data.
The stored data can be accessed during lifetime of a session. Consider the
following script, which creates a new session and registers two session
variables.
<?php
// Starting session
session_start();
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>
Destroying a Session
to destroy a session completely, simply call the session_destroy() function.
This function does not need any argument and a single call destroys all the
session data.
Destroying a session in PHP refers to the process of deleting all session data
associated with a particular user session. This includes removing session variables,
invalidating the session ID, and deleting the session cookie (if used). Destroying a
session effectively logs the user out and removes any stored information related to
that session.
Example:
<?php
// Starting session session_start();
// Destroying session session_destroy();
?>
7.Difference between cookies and session.