0% found this document useful (0 votes)
7 views10 pages

Chapter No 4

This document discusses PHP forms and validation. It covers the role of the PHP server, how to create cookies using setcookie(), defines cookies and sessions, describes the PHP mail function, and discusses the differences between GET and POST methods, creating multiple forms on a page, submitting forms with multiple buttons, and managing cookies and sessions.

Uploaded by

Fazal Qureshi
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)
7 views10 pages

Chapter No 4

This document discusses PHP forms and validation. It covers the role of the PHP server, how to create cookies using setcookie(), defines cookies and sessions, describes the PHP mail function, and discusses the differences between GET and POST methods, creating multiple forms on a page, submitting forms with multiple buttons, and managing cookies and sessions.

Uploaded by

Fazal Qureshi
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/ 10

Chapter No 4: Creating and validating forms

2 mark

1. What is role of server in PHP?


Answer:

A PHP server refers to a web server software or environment that is


configured to process PHP (Hypertext Preprocessor) scripts.

Roles

It enables the execution of PHP code, facilitating the creation of dynamic


web applications and websites.

When a user accesses a PHP-powered web page, the PHP server processes
the embedded PHP scripts within the HTML content.

It dynamically generates HTML output by executing the PHP code and


interacting with databases, APIs, or other external resources as needed.

2. How to create cookies.

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.

A cookie is created with the setcookie() function.

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 : A session is used to save information on the server momentarily


so that it may be utilized across various pages of the website. It is the
overall amount of time spent on an activity. The user session begins when
the user logs in to a specific network application and ends when the user
logs out of the program or shuts down the machine.

Session values are far more secure since they are saved in binary or
encrypted form

4. Describe the PHP mail function

Answer:
The mail() function allows you to send emails directly from a script.

PHP makes use of mail() function to send an email. This function


requires three mandatory arguments that specify the recipient's
email address, the subject of the the message and the actual
message additionally there are other two optional parameters.

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

5. How top start session, get session and


destroy session.
Answer

Starting a PHP Session


Before you can store any information in session variables, you must first start
up the session. To begin a new session, simply call the
PHP session_start() function. It will create a new session and generate a
unique session ID for the user.

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();

// Storing session data

$_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.

You might also like