0% found this document useful (0 votes)
2 views3 pages

PHP and JSP Lab

The document provides several examples of PHP programs, including a foreach loop, form validation, file upload, session creation, and cookie management. Each example is accompanied by HTML code demonstrating its functionality. Additionally, there is a mention of JSP at the end, but no details are provided.

Uploaded by

Srinivas Boini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

PHP and JSP Lab

The document provides several examples of PHP programs, including a foreach loop, form validation, file upload, session creation, and cookie management. Each example is accompanied by HTML code demonstrating its functionality. Additionally, there is a mention of JSP at the end, but no details are provided.

Uploaded by

Srinivas Boini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

PHP for each loop example program

<html>
<body>

<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
}

$myCar = new Car("red", "Volvo");

foreach ($myCar as $x => $y) {


echo "$x: $y<br>";
}
?>

</body>
</html>

2. Php form validation program

<html><body>
<form action="welcome.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br><input
type="submit"></form>
</body><
/html>

Welcome.php program

<html><body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>
</body></html>

3. Php file upload program

<html>
<body>

<form action="upload.php" method="post"


enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

Upload.php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]
["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>

4. php program for creating session.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

5. Php proram creating Cookie


<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

6. JSP

You might also like