0% found this document useful (0 votes)
19 views14 pages

WBP Utii QB

Uploaded by

sawantkrish562
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)
19 views14 pages

WBP Utii QB

Uploaded by

sawantkrish562
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/ 14

WBP(22619) UNIT TEST II QUESTION BANK

Q1) Explain serialization with example


Serialization

Serialization is the process of converting an object into a byte stream so it can


be stored in a file, database, or sent over a network. Deserialization is the
reverse process, where the byte stream is converted back into an object.

Example in PHP

1. <?Php
2. Class Student {
3. Public $name, $age;
4. Public function __construct($name, $age) {
5. $this->name = $name;
6. $this->age = $age;
7. }
8. }
9. $student = new Student("John", 20);
10. $serializeddata = serialize($student); // Convert object to string
11. Echo $serializeddata;
12. $unserializeddata = unserialize($serializeddata); // Convert back to object
13. Echo "\nname: " . $unserializeddata->name;
14. ?>

Output:

1. O:7:"Student":2:{s:4:"name";s:4:"John";s:3:"age";i:20;}
2. Name: John

Q2) Compare session and cookie in 6 easy and short


points in tabular format

Feature Session Cookie

Storage Stored on the server Stored in the user's browser

Security More secure Less secure

Size
No size limit Limited to 4KB
Limit

1|Page
WBP(22619) UNIT TEST II QUESTION BANK

Feature Session Cookie

Ends when the browser is closed


Lifetime Can persist for a long time
or session expires

Accessible by both client and


Access Accessible only on the server
server

Used for storing sensitive data Used for storing user


Usage
(e.g., login info) preferences (e.g., theme)

Q3) List out database operation


Database Operations

1. Create – Insert new records into a database.

o Example: INSERT INTO users (name, email) VALUES ('John',


'john@example.com');

2. Read (Retrieve) – Fetch data from the database.

o Example: SELECT * FROM users WHERE id = 1;

3. Update – Modify existing records.

o Example: UPDATE users SET email = 'newemail@example.com'


WHERE id = 1;

4. Delete – Remove records from the database.

o Example: DELETE FROM users WHERE id = 1;

2|Page
WBP(22619) UNIT TEST II QUESTION BANK

Q4) Explain can we destroy cookie


Can We Destroy a Cookie?

Yes, a cookie can be destroyed by setting its expiration time in the past. This
removes the cookie from the user's browser.

Syntax to Destroy a Cookie in PHP:

1. Setcookie("user", "", time() - 3600, "/"); // Expire the cookie

Example:

1. <?Php
2. Setcookie("username", "johndoe", time() + 3600, "/");
3. Echo "Cookie is set.<br>";
4. Setcookie("username", "", time() - 3600, "/");
5. Echo "Cookie is deleted.";
6. ?>

How It Works?

1. The first setcookie() creates a cookie with a 1-hour lifespan.

2. The second setcookie() sets the expiration time in the past (time() -
3600), deleting the cookie.

Q5) Explain introspection with example


Introspection in PHP

Introspection is the ability of a program to examine its own structure, such as


checking the properties, methods, and class of an object at runtime.

Common Introspection Functions in PHP:

1. Get_class($object) – Gets the class name of an object.

2. Get_class_methods($class) – Lists all methods of a class.

3. Get_class_vars($class) – Lists all properties of a class.

4. Get_object_vars($object) – Lists all properties of an object.

3|Page
WBP(22619) UNIT TEST II QUESTION BANK

5. Method_exists($object, "methodname") – Checks if a method exists.

6. Property_exists($object, "propertyname") – Checks if a property exists.

Example: Introspection in PHP

1. <?Php
2. Class Person {
3. Public $name = "John";
4. Private $age = 25;
5. Public function greet() {
6. Return "Hello!";
7. }
8. }
9.
10. $obj = new Person();
11. Echo "Class Name: " . Get_class($obj) . "<br>";
12. Print_r(get_class_methods($obj));
13. Echo "<br>";
14. Print_r(get_class_vars("Person"));
15. Echo "<br>";
16. If(method_exists($obj, "greet")) {
17. Echo "Method 'greet' exists.";
18. }
19. ?>

Output:

1. Class Name: Person


2. Array ( [0] => greet )
3. Array ( [name] => John )
4. Method 'greet' exists.

Q6) Explain Mail() in php with example


PHP mail() Function

The mail() function in PHP is used to send emails from a script. It requires a
properly configured mail server and supports additional headers and
parameters for customization. It returns true on success and false on failure.

Syntax:

1. Mail(to, subject, message, headers, parameters);

4|Page
WBP(22619) UNIT TEST II QUESTION BANK

• To – Recipient's email address

• Subject – Email subject

• Message – Email body

• Headers – Additional headers (e.g., From, CC, BCC)

• Parameters (optional) – Extra parameters for mail sending

Example: Sending an Email

1. <?Php
2. $to = "example@example.com";
3. $subject = "Test Email";
4. $message = "Hello, this is a test email from PHP!";
5. $headers = "From: sender@example.com";
6. If(mail($to, $subject, $message, $headers)) {
7. Echo "Email sent successfully!";
8. } else {
9. Echo "Email sending failed.";
10. }
11. ?>

Q7) Explain web page validation with example


Web Page Validation

Web page validation ensures that user input is correct, secure, and meets the
required format before processing. It improves data integrity and security.

1. <?Php
2. If ($_SERVER["REQUEST_METHOD"] == "POST") {
3. If (empty($_POST["name"])) {
4. Echo "Name is required!";
5. } else {
6. Echo "Welcome, " . Htmlspecialchars($_POST["name"]);
7. }
8. }
9. ?>

5|Page
WBP(22619) UNIT TEST II QUESTION BANK

10. <form method="post">


11. Name: <input type="text" name="name">
12. <input type="submit" value="Submit">
13. </form>
14. <?Php
15. $servername = "localhost";
16. $username = "root"; // Change if needed
17. $password = ""; // Change if needed
18. $database = "mydatabase"; // Change to your database name
19. $conn = new mysqli($servername, $username, $password, $database);
20. If ($conn->connect_error) {
21. Die("Connection failed: " . $conn->connect_error);
22. }
23. $id = 1;
24. $sql = "DELETE FROM users WHERE id=$id";
25. If ($conn->query($sql) === TRUE) {
26. Echo "Record deleted successfully!";
27. } else {
28. Echo "Error deleting record: " . $conn->error;
29. }
30. $conn->close();
31. ?>

Q8) Write php program to demonstrate delete operation


on table data
PHP Code:

1. <?Php
2. $servername = "localhost";
3. $username = "root"; // Change if needed
4. $password = ""; // Change if needed
5. $database = "mydatabase"; // Change to your database name
6. $conn = new mysqli($servername, $username, $password, $database);
7. If ($conn->connect_error) {
8. Die("Connection failed: " . $conn->connect_error);
9. }
10. $id = 1;
11. $sql = "DELETE FROM users WHERE id=$id";
12. If ($conn->query($sql) === TRUE) {
13. Echo "Record deleted successfully!";
14. } else {
15. Echo "Error deleting record: " . $conn->error;
16. }
17. $conn->close();
18. ?>

6|Page
WBP(22619) UNIT TEST II QUESTION BANK

SQL Table Example (users Table):

Id Name Email

1 John John@example.com

2 Alice Alice@example.com

Q9) Wap to demonstrate the use of cookie


PHP Code:

1. <?Php
2. Setcookie("user", "johndoe", time() + 3600, "/");
3. If(isset($_COOKIE["user"])) {
4. Echo "Welcome, " . $_COOKIE["user"];
5. } else {
6. Echo "Cookie is not set!";
7. }
8. Setcookie("user", "", time() - 3600, "/");
9. ?>

Output (First Visit):

1. Welcome, johndoe

After Cookie Expiry or Deletion:

1. Cookie is not set!

7|Page
WBP(22619) UNIT TEST II QUESTION BANK

Q10) Wap to demonstrate db operation in php


PHP Code:

1. <?Php
2. $conn = new mysqli("localhost", "root", "", "mydatabase");
3. If ($conn->connect_error) {
4. Die("Connection failed!");
5. }
6. $conn->query("INSERT INTO users (name, email) VALUES ('John', 'john@example.com')");
7. $result = $conn->query("SELECT * FROM users");
8. While ($row = $result->fetch_assoc()) {
9. Echo $row["id"] . " - " . $row["name"] . " - " . $row["email"] . "<br>";
10. }
11. $conn->query("UPDATE users SET name='Jane' WHERE name='John'");
12. $conn->query("DELETE FROM users WHERE name='Jane'");
13. $conn->close();
14. ?>

Q11) List attributes of cookie with short desc


Attributes of a Cookie

Attribute Description

Name Name of the cookie.

Value Data stored in the cookie.

Expires Expiry time (timestamp) after which the cookie is deleted.

Path Defines where the cookie is accessible in the domain.

Domain Specifies the domain for which the cookie is valid.

Secure Ensures the cookie is sent only over HTTPS.

Httponly Restricts access to the cookie from javascript, enhancing security.

8|Page
WBP(22619) UNIT TEST II QUESTION BANK

Q12) Explain cloning object


Cloning an Object in PHP

Cloning an object creates a copy of an existing object while keeping them


independent of each other.

Syntax:

1. $newobject = clone $oldobject;

Example:

1. <?Php
2. Class Car {
3. Public $brand;
4. Public function __construct($brand) {
5. $this->brand = $brand;
6. }
7. }
8. $car1 = new Car("Toyota");
9. $car2 = clone $car1;
10. Echo $car1->brand; // Toyota
11. Echo "<br>";
12. Echo $car2->brand; // Toyota
13. ?>

Q13) Define mysql


Mysql Definition

Mysql is an open-source relational database management system (RDBMS)


used to store, manage, and retrieve structured data efficiently. It uses SQL
(Structured Query Language) for database operations.

Key Features:

9|Page
WBP(22619) UNIT TEST II QUESTION BANK

• Fast and scalable

• Supports multiple users

• Secure and reliable

• Works with PHP, Python, Java, etc.

Q14) Develop a simple program for sending and


receiving plain text message
1. Create Database Table

Run this SQL query in phpmyadmin or mysql CLI:

1. CREATE DATABASE messages_db;


2. USE messages_db;
3. CREATE TABLE messages (
4. Id INT AUTO_INCREMENT PRIMARY KEY,
5. Message TEXT NOT NULL
6. );

2. PHP Code (index.php)

1. <?Php
2. $conn = new mysqli("localhost", "root", "", "messages_db");
3. If ($conn->connect_error) {
4. Die("Connection failed!");
5. }
6. If ($_SERVER["REQUEST_METHOD"] == "POST") {
7. $msg = $_POST["message"];
8. $conn->query("INSERT INTO messages (message) VALUES ('$msg')");
9. }
10. $result = $conn->query("SELECT * FROM messages");
11. ?>
12. <!DOCTYPE html>
13. <html>
14. <head>
15. <title>Simple Chat</title>
16. </head>
17. <body>
18. <h2>Send Message</h2>
19. <form method="post">
20. <input type="text" name="message" required>
21. <button type="submit">Send</button>

10 | P a g e
WBP(22619) UNIT TEST II QUESTION BANK

22. </form>
23. <h2>Messages</h2>
24. <?Php while ($row = $result->fetch_assoc()) { ?>
25. <p><?Php echo $row["message"]; ?></p>
26. <?Php } ?>
27. </body>
28. </html>
29.

Q15) Design a webpage using all form controls


Form controls are interactive elements that allow users to input and submit
data in a web form. They help collect different types of user input, such as text,
numbers, selections, and files.

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form Controls Example</title>
5. </head>
6. <body>
7. <h2>Webpage with All Form Controls</h2>
8. <form action="submit.php" method="post">
9. <label for="text">Text Input:</label>
10. <input type="text" id="text" name="text"><br><br>
11. <label for="password">Password:</label>
12. <input type="password" id="password" name="password"><br><br>
13. <label for="email">Email:</label>
14. <input type="email" id="email" name="email"><br><br>
15. <label for="date">Date:</label>
16. <input type="date" id="date" name="date"><br><br>
17. <label>Gender:</label>
18. <input type="radio" id="male" name="gender" value="Male">
19. <label for="male">Male</label>
20. <input type="radio" id="female" name="gender" value="Female">
21. <label for="female">Female</label><br><br>
22. <label>Hobbies:</label>
23. <input type="checkbox" id="reading" name="hobby[]" value="Reading">
24. <label for="reading">Reading</label>
25. <input type="checkbox" id="sports" name="hobby[]" value="Sports">
26. <label for="sports">Sports</label><br><br>
27. <label for="dropdown">Select Country:</label>
28. <select id="dropdown" name="country">
29. <option value="India">India</option>
30. <option value="USA">USA</option>
31. <option value="UK">UK</option>

11 | P a g e
WBP(22619) UNIT TEST II QUESTION BANK

32. </select><br><br>
33. <label for="textarea">Message:</label>
34. <textarea id="textarea" name="message" rows="4" cols="30"></textarea><br><br>
35. <label for="tel">Phone:</label>
36. <input type="tel" id="tel" name="tel"><br><br>
37. <input type="submit" value="Submit">
38. <input type="reset" value="Reset">
39. </form>
40. </body>
41. </html>

Q16) Compare GET and POST

Feature GET Method POST Method

Data Visibility Visible in URL Hidden from URL

Security Less secure (data in URL) More secure (data in body)

Limited (URL length


Data Length Unlimited
restriction)

Used for sending sensitive


Usage Used for fetching data
data

Bookmarking Can be bookmarked Cannot be bookmarked

Performance Faster (cached by browsers) Slightly slower (not cached)

Q17) explain webserver role in web development


Role of Web Server in Web Development

A web server is software or hardware that handles requests from clients


(browsers) and serves web pages, files, or applications. It plays a crucial role
in delivering web content to users.

Key Roles of a Web Server:

12 | P a g e
WBP(22619) UNIT TEST II QUESTION BANK

Role Description

Processing
Accepts and processes HTTP requests from browsers.
Requests

Serving Web Sends HTML, CSS, JS, images, and other resources to
Pages users.

Handling Security Implements SSL/TLS for secure data transmission.

Executing Scripts Runs server-side scripts like PHP, Python, or Node.js.

Managing Connects to databases (mysql, postgresql) to fetch and


Databases update data.

Distributes traffic across multiple servers for better


Load Balancing
performance.

Logging &
Tracks requests, errors, and performance for debugging.
Monitoring

Q18) Explain Session in PHP


Session in PHP

A session in PHP is a way to store user data across multiple pages. Unlike
cookies, session data is stored on the server, making it more secure. Sessions
are initiated using session_start() and data is accessed via the $_SESSION
superglobal.

Key Features of PHP Sessions:

• Stores user information (e.g., login details) until the session ends.

• Data is stored on the server, not in the browser.

• A unique session ID is assigned to each user.

13 | P a g e
WBP(22619) UNIT TEST II QUESTION BANK

• Sessions expire after a set time or when the user logs out.

Syntax & Example:

Start a Session:

1. Session_start(); // Start the session


2. $_SESSION["username"] = "John"; // Store data
3. Echo "Session set!";

Access Session Data:

1. Session_start();
2. Echo "Welcome, " . $_SESSION["username"];

Destroy a Session:

1. Session_start();
2. Session_destroy(); // Ends the session

Q19) Wap to connect php with mysql


1. <?Php
2. $host = "localhost";
3. $user = "root";
4. $pass = "";
5. $dbname = "testdb";
6. $conn = new mysqli($host, $user, $pass, $dbname);
7. If ($conn->connect_error) {
8. Die("Connection failed");
9. } else {
10. Echo "Connected successfully";
11. }
12. $conn->close();
13. ?>

14 | P a g e

You might also like