Ankit 09 Assignment 2 PHP
Ankit 09 Assignment 2 PHP
ASSIGNMENT :02
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
</form>
<?php
function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
if ($number >= 0) {
$result = factorial($number);
} else {
?>
</body>
</html>
OUTPUT:
2. Passing by Value and Reference: Create a PHP script that
demonstrates passing variables by value and by reference in
functions. Display how modifying a variable within the
function behaves differently in each case.
CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Passing by Value and Reference</title>
</head>
<body>
<h1>Passing by Value and Reference in PHP</h1>
<?php
// Function that modifies the variable (passing by value)
function modifyByValue($num) {
$num += 10;
echo "<p>Inside modifyByValue: \$num is $num</p>";
}
</body>
</html>
OUTPUT:
3. Working with Forms (GET and POST Methods): Design a
login form that collects a username and password. Validate the
inputs using PHP and display a success or error message based
on the user's input using the POST method.
CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<?php
// Predefined username and password (For demo purposes)
$validUsername = "user123";
$validPassword = "password123";
Output:
CODE:
<!-- feedback_form.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>User Feedback Form</title>
</head>
<body>
<h2>User Feedback Form</h2>
<label for="email">Email:</label>
<input type="email" name="email"><br><br>
<label for="comments">Comments:</label><br>
<textarea name="comments" rows="5" cols="30"
required></textarea><br><br>
CODE:
<!-- display_feedback.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Display Feedback</title>
</head>
<body>
<h2>Collected User Feedback</h2>
<?php
// Check if all data is present
if (isset($_GET['name']) && isset($_GET['email']) &&
isset($_GET['rating']) && isset($_GET['comments'])) {
// Retrieve data and sanitize
$feedback = [
'name' => htmlspecialchars($_GET['name']),
'email' => htmlspecialchars($_GET['email']),
'rating' => htmlspecialchars($_GET['rating']),
'comments' => htmlspecialchars($_GET['comments'])
];
OUTPUT:
5. Cookies: Create a PHP script that sets a cookie to store the
user’s preferred language (e.g., English, French, Spanish).
The next time the user visits the page, display a greeting
message in the selected language using the stored cookie.
CODE:
<!-- language_selection.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Select Your Preferred Language</title>
</head>
<body>
<?php
// Check if the user has already set a preferred language cookie
if (isset($_COOKIE['language'])) {
// Retrieve the language from the cookie
$language = $_COOKIE['language'];
<?php
// Set the cookie if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" &&
isset($_POST['language'])) {
// Set the cookie for 30 days
$selectedLanguage = $_POST['language'];
setcookie('language', $selectedLanguage, time() + (30 * 24 * 60 * 60));
// Cookie valid for 30 days
</body>
</html>
OUTPUT:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Simple Shopping Cart</title>
</head>
<body>
<h2>Shopping Page</h2>
</body>
</html>
<!-- cart.php -->
<?php
session_start();
// Initialize message
$message = '';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Shopping Cart</title>
</head>
<body>
</body>
</html>
CODE:
<!-- login.php -->
<?php
session_start(); // Start a session
// Simple authentication
if ($username === $valid_username && $password ===
$valid_password) {
$_SESSION['username'] = $username; // Store username in
session
header("Location: welcome.php"); // Redirect to welcome page
exit();
} else {
$error_message = "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
</body>
</html>
<!-- welcome.php -->
<?php
session_start(); // Start a session
// Handle logout
if (isset($_GET['logout'])) {
session_destroy(); // Destroy the session
header("Location: login.php"); // Redirect to login page
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Page</title>
</head>
<body>
</body>
</html>