0% found this document useful (0 votes)
12 views

PHPPRAC

The documents discuss form validation and session management in PHP. They demonstrate how to validate user input on forms, such as validating names, genders, terms and conditions, emails, and how to manage user sessions by logging users in and out of the site.

Uploaded by

Saima Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PHPPRAC

The documents discuss form validation and session management in PHP. They demonstrate how to validate user input on forms, such as validating names, genders, terms and conditions, emails, and how to manage user sessions by logging users in and out of the site.

Uploaded by

Saima Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PRAC 10:

<!DOCTYPE html>

<head>

<title>Form Validation</title>

</head>

<body>

<?php

$nameErr = $genderErr = $termsErr = "";

$name = $gender = $terms = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {

$nameErr = "Name is required";

else {

$name = test_input($_POST["name"]);

if (empty($_POST["gender"])) {

$genderErr = "Gender is required";

else {

$gender = test_input($_POST["gender"]);

if (!isset($_POST["terms"])) {

$termsErr = "You must agree to the terms";

}
else {

$terms = test_input($_POST["terms"]);

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">

<span class="error"><?php echo $nameErr;?></span>

<br><br>

Gender:

<input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="male">Male

<span class="error"><?php echo $genderErr;?></span>

<br><br>

<input type="checkbox" name="terms" value="agree"> I agree to the terms

<span class="error"><?php echo $termsErr;?></span>

<br><br>

<input type="submit" name="submit" value="Submit">


</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST" && empty($nameErr) && empty($genderErr) &&


empty($termsErr)) {

echo "<h2>Validation Successful!</h2>";

echo "<p>Name: $name</p>";

echo "<p>Gender: $gender</p>";

echo "<p>Agreed to Terms: $terms</p>";

?>

</body>

</html>

practical no 11:

<!DOCTYPE html>
<head>

<title>Form Validation</title>

</head>

<body>

<?php

$nameErr = $genderErr = $termsErr = "";

$name = $gender = $terms = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {

$nameErr = "Name is required";

else {

$name = test_input($_POST["name"]);

if (empty($_POST["gender"])) {

$genderErr = "Gender is required";

else {

$gender = test_input($_POST["gender"]);

if (!isset($_POST["terms"])) {

$termsErr = "You must agree to the terms";

else {

$terms = test_input($_POST["terms"]);

function test_input($data) {
$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">

<span class="error"><?php echo $nameErr;?></span>

<br><br>

Gender:

<input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="male">Male

<span class="error"><?php echo $genderErr;?></span>

<br><br>

<input type="checkbox" name="terms" value="agree"> I agree to the terms

<span class="error"><?php echo $termsErr;?></span>

<br><br>

<input type="submit" name="submit" value="Submit">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST" && empty($nameErr) && empty($genderErr) &&


empty($termsErr)) {

echo "<h2>Validation Successful!</h2>";

echo "<p>Name: $name</p>";

echo "<p>Gender: $gender</p>";

echo "<p>Agreed to Terms: $terms</p>";


}

?>

</body>

</html>

PRACTICAL NO: 12

<?php

function isEmailValid($email) {

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/";

return preg_match($pattern, $email);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$enteredEmail = $_POST["email"];

if (isEmailValid($enteredEmail)) {

$resultMessage = "Email is valid!";

else {

$resultMessage = "Invalid email address!";

}
}

?>

<!DOCTYPE html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Email Validation</title>

</head>

<body>

<h2>Email Validation</h2>

<?php

if (isset($resultMessage)) {

echo "<p>$resultMessage</p>";

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

<label for="email">Enter Email:</label>

<input type="text" id="email" name="email" required>

<button type="submit">Check Email</button>

</form>

</body>

</html>
PRACTICAL NO: 13

<?php

session_start();

if (isset($_SESSION['username'])) {

$username = $_SESSION['username'];

$message = "Welcome back, $username!";

else {

$message = "Welcome, guest!";

if (isset($_GET['logout'])) {

session_destroy();

header("Location: " . strtok($_SERVER["REQUEST_URI"], '?'));

exit();

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$username = $_POST["username"];

$_SESSION['username'] = $username;

header("Location: " . strtok($_SERVER["REQUEST_URI"], '?'));


exit();

?>

<!DOCTYPE html>

<head>

<title>Session Management</title>

</head>

<body>

<h2><?php echo $message; ?></h2>

<?php if (isset($_SESSION['username'])) : ?>

<p><a href="?logout">Logout</a></p>

<?php else : ?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

<label for="username">Enter Username:</label>

<input type="text" id="username" name="username" required>

<button type="submit">Login</button>

</form>

<?php endif; ?>

</body>

</html>

You might also like