tybcs_solution
tybcs_solution
Q. 1)Write a PHP script to keep track of number of times the web page has been
accessed (Use SessionTracking)
<?php
// Start the session
session_start();
-----------------------------------------------------------------------------------
------------------------------------------------------
slip 2:
<!DOCTYPE html>
<html>
<head>
<title>Preferences</title>
</head>
<body>
<?php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Save preferences in cookies
setcookie("font_style", $_POST['font_style'], time() + (86400 * 30), "/");
setcookie("font_size", $_POST['font_size'], time() + (86400 * 30), "/");
setcookie("font_color", $_POST['font_color'], time() + (86400 * 30), "/");
setcookie("bg_color", $_POST['bg_color'], time() + (86400 * 30), "/");
// Redirect to display preferences page
header("Location: {$_SERVER['PHP_SELF']}");
exit();
}
// Reset preferences when reset button is clicked
if (isset($_POST['reset'])) {
setcookie("font_style", "", time() - 3600, "/");
setcookie("font_size", "", time() - 3600, "/");
setcookie("font_color", "", time() - 3600, "/");
setcookie("bg_color", "", time() - 3600, "/");
header("Location: {$_SERVER['PHP_SELF']}");
exit();
}
?>
<?php
// Display form to set preferences
if (!isset($_COOKIE['font_style'])) {
// If preferences are not set, show the form
?>
<h2>Select Your Preferences</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="font_style">Font Style:</label>
<select name="font_style">
<option value="Arial">Arial</option>
<option value="Times New Roman">Times New Roman</option>
<option value="Verdana">Verdana</option>
</select><br><br>
<label for="font_size">Font Size:</label>
<select name="font_size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select><br><br>
<label for="font_color">Font Color:</label>
<input type="color" name="font_color"><br><br>
<label for="bg_color">Background Color:</label>
<input type="color" name="bg_color"><br><br>
<input type="submit" value="Save Preferences">
</form>
<?php
} else {
// If preferences are set, display them
?>
<h2>Selected Preferences</h2>
<div style="font-family:<?php echo $_COOKIE['font_style']; ?>;
font-size:<?php echo $_COOKIE['font_size']; ?>;
color:<?php echo $_COOKIE['font_color']; ?>;
background-color:<?php echo $_COOKIE['bg_color']; ?>;
padding: 20px;">
This is a sample text with selected preferences.
</div>
<br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="reset" value="true">
<input type="submit" value="Reset Preferences">
</form>
<?php
}
?>
</body>
</html>
-----------------------------------------------------------------------------------
------------------------------------------------------
slip 3:
Login.php
<?php
session_start();
// Initialize variables
$max_attempts = 3;
$valid_username = "user"; // Change this to your valid username
$valid_password = "password"; // Change this to your valid password
$error = "";
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
// Check if username and password are correct
if ($username == $valid_username && $password == $valid_password) {
$_SESSION["authenticated"] = true;
header("Location: welcome.php");
exit();
} else {
$_SESSION["attempts"] = isset($_SESSION["attempts"]) ? $_SESSION["attempts"] + 1 :
1;
$attempts_left = $max_attempts - $_SESSION["attempts"];
if ($_SESSION["attempts"] >= $max_attempts) {
$error = "Maximum login attempts reached. Please try again later.";
session_destroy();
} else {
$error = "Invalid username or password. You have $attempts_left attempt(s) left.";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Form</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
<p style="color: red;"><?php echo $error; ?></p>
</body>
</html>
Welcome.php
<?php
session_start();
// Check if user is authenticated
if (!isset($_SESSION["authenticated"]) || $_SESSION["authenticated"] !== true) {
// If not authenticated, redirect to login page
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome!</h2>
<p>You have successfully logged in.</p>
</body>
</html>
-----------------------------------------------------------------------------------
------------------------------------------------------
slip 4:
-----------------------------------------------------------------------------------
------------------------------------------------------
slip 5 :
php file:
<?php
$xml = simplexml_load_file("slip6.xml");
echo "<br>Elements:<br>";
foreach ($xml->book[0]->children() as $child) {
echo $child->getName() . ": " . $child . "<br>";
}
?>
xml file:
<?xml version="1.0" encoding="UTF8"?>
<library>
<book id="1">
<title>The catcher in the Rye </title>
<author> J.D.Salinger</author>
<year>1951</year>
</book>
<br><br>
<book id="2">
<title>To Kill a Mockingbird</title>
<author>Haper Lee</author>
<year>1960</year>
</book>
<library>
-----------------------------------------------------------------------------------
----------------------------------------------------
slip 7:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 8:
Q. 1)Write a JavaScript to display message ‘Exams are near, have you started
preparing for?’ (usealert box ) and Accept any two numbers from user and display
addition of two number .(Use Prompt andconfirm box)
Slip8.html
<!DOCTYPE html>
<head>
<title>JavaScript Task</title>
<script>
// Display an alert with a message
alert("Exams are near, have you started preparing for?");
// Convert the prompt inputs to numbers (as they are initially strings)
num1 = parseFloat(num1);
num2 = parseFloat(num2);
// Use confirm box to ask if the user wants to calculate the sum
var userConfirm = confirm("Do you want to calculate the addition of the two
numbers?");
if (userConfirm) {
// If the user confirms, calculate and display the sum
var sum = num1 + num2;
alert("The sum of the two numbers is: " + sum);
} else {
// If the user cancels, display a message
alert("You chose not to calculate the sum.");
}
</script>
</head>
<body>
</body>
</html>
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 9:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Membership Form</title>
<script>
function validateForm() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
<h2>Membership Form</h2>
</body>
</html>
-----------------------------------------------------------------------------------
-----------------------------------------------------
sip 10:
<!DOCTYPE html>
<html>
<head>
<title>Insert Text Before and After Paragraph</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
$("#insertText").click(function() {
$("#myParagraph").before("<b>Text before paragraph: </b>");
$("#myParagraph").after("<b>Text after paragraph: </b>");
});
});
</script>
</body>
</html>
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 11:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 12:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 13:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 14:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 15:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 16:
-----------------------------------------------------------------------------------
-----------------------------------------------------
slip 17:
Q. 1) Write a Java Script Program to show Hello Good Morning message onload event
using alert box
and display the Student registration from. [Marks 15]
<html>
<head>
<title>Hello Good Morning</title>
<script> window.onload = function () {
alert("Hello! Good Morning");
};
function showRegistrationForm() {
document.getElementById("registrationForm").style.display = "block";
}
</script>
</head>
<body>
<h2>Student Registration Form</h2>
<form>
Name:
<input type="text" id="name" name="name"><br><br>
Email:
<input type="email" id="email" name="email"><br><br>
Phone:
<input type="text" id="phone" name="phone"><br><br>
<input type="submit" value="Submit"> </form>
</div>
</html>
-----------------------------------------------------------------------------------
---------------------------------------------------
slip 18:
<!DOCTYPE html>
<html>
<head>
<title>Simple Fibonacci Generator</title>
</head>
<body>
<h1>Fibonacci Numbers</h1>
<button onclick="generateFibonacci()">Generate Fibonacci</button>
<p id="output"></p>
<script>
function generateFibonacci() {
var n = parseInt(prompt("Enter the number of terms:"));
var n1 = 0, n2 = 1, result = "";
document.getElementById("output").textContent = result;
}
</script>
</body>
</html>
-----------------------------------------------------------------------------------
----------------------------------------------------
slip 19 :
slip19.html
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h1>User Login</h1>
<form id="myForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
<script src="Slip19.js"></script>
</body>
</html>
slip19.js
const form = document.getElementById('myForm');
form.addEventListener('submit', (e) =>
{
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (username === '' || password === '')
{
alert('Please enter both username and password.');
e.preventDefault();
} else if (username.length < 4 || username.length > 20)
{
alert('Username must be between 4 and 20 characters.');
e.preventDefault();
} else if (password.length < 8)
{
alert('Password must be at least 8 characters.');
e.preventDefault();
} else
{
alert('Form submitted successfully!');
}
});
-----------------------------------------------------------------------------------
----------------------------------------------------
slip 20:
-----------------------------------------------------------------------------------
---------------------------------------------------
slip 21:
-----------------------------------------------------------------------------------
----------------------------------------------------
slip 22:
slip 23:
-----------------------------------------------------------------------------------
----------------------------------------------------
slip 24:
slip24A.php
<?php
// Create student.xml file if it doesn't exist
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"
?><students></students>');
$student3 = $xml->addChild('student');
$student3->addChild('rollno', '103');
$student3->addChild('name', 'Michael Brown');
$student3->addChild('address', '789 Oak St');
$student3->addChild('college', 'XYZ University');
$student3->addChild('course', 'Computer Science');
slip24B.php
<?php
// Load the XML file
$xml = simplexml_load_file('student.xml');
// Initialize variables
$course = "";
$students = [];
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Details</title>
</head>
<body>
<h1>Student Details by Course</h1>
<?php
// Initialize variables
$fibResult = "";
$sumResult = 0;
$a = 0;
$b = 1;
$fibResult = "Fibonacci Series: ";
for ($i = 0; $i < $number; $i++) {
$fibResult .= $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}
<!DOCTYPE html>
<head>
</body>
</html>
-----------------------------------------------------------------------------------
------------------------------------------------------
slip 30:
-----------------------------------------------------------------------------------
-------------------------------------------------------