0% found this document useful (0 votes)
36 views5 pages

PHP

Uploaded by

aaronpinheiro84
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)
36 views5 pages

PHP

Uploaded by

aaronpinheiro84
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/ 5

1.

Write a PHP program to store current date/time in a cookie and display the 'last visited on'
date/time on the web page upon reopening of the same page.

<?php

// Set a cookie for the current date/time, which expires in 1 year (365 days)

$cookie_name = "lastVisit";

$cookie_value = date("Y-m-d H:i:s"); // Store the current date and time in 'YYYY-MM-DD
HH:MM:SS' format

$expiry_time = time() + (365 * 24 * 60 * 60); // Cookie expiry set to 1 year

// If the cookie exists, retrieve the value and store it in a variable

if(isset($_COOKIE[$cookie_name])) {

$last_visited = $_COOKIE[$cookie_name];

echo "Last visited on: " . $last_visited . "<br>";

} else {

echo "This is your first visit!<br>";

// Update the cookie with the current date/time

setcookie($cookie_name, $cookie_value, $expiry_time);

?>

<!DOCTYPE html>

<html >

<head>

<title>Last Visit Example</title>

</head>

<body>

<h1>Welcome to the Page!</h1>

</body>

</html>
2. Write a program to demonstrate session.
Create_session.php

<?php
// Start the session
session_start();

// Store session data


$_SESSION["username"] = "Nithin";
$_SESSION["email"] = "[email protected]";
?>
<html>
<head>

<title>Create Session</title>
</head>
<body>
<h1>Session Created</h1>
<p>Username and email have been stored in the session.</p>
<a href="view_session.php">View Session Data</a>
</body>
</html>

view_session.php

<?php
// Start the session
session_start();

// Check if session variables are set


if (isset($_SESSION["username"]) && isset($_SESSION["email"])) {
$username = $_SESSION["username"];
$email = $_SESSION["email"];
} else {
$username = "No session data found.";
$email = "No session data found.";
}
?>
<html>
<head>

<title>View Session</title>
</head>
<body>
<h1>View Session Data</h1>
<p><strong>Username:</strong> <?php echo $username; ?></p>
<p><strong>Email:</strong> <?php echo $email; ?></p>
<a href="destroy_session.php">Destroy Session</a>
</body>
</html>

destroy_session.php

<?php
// Start the session
session_start();

// Destroy the session


session_destroy();
?>

<html>
<head>

<title>Destroy Session</title>
</head>
<body>
<h1>Session Destroyed</h1>
<p>The session data has been deleted.</p>
<a href="view_session.php">View Session Data</a> <!-- This will show no data since the
session has been destroyed -->
</body>
</html>

3. Create a PHP program to display the biodata of a person in a table format, by reading the
personal details using an HTML page.

Biodata.html

<!DOCTYPE html>
<html>
<head>
<title>Biodata Form</title>
</head>
<body>
<h1>Biodata Form</h1>
<form action="biodata.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" required></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="number" name="age" required></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="address" required></textarea></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="tel" name="phone" required></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

Biodata.php

<html>
<head>
<title>Biodata</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid;
padding: 10px;
}
</style>
</head>
<body>
<h1>Biodata</h1>
<table>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
<tr>
<td>Name:</td>
<td><?php echo $_POST['name']; ?></td>
</tr>
<tr>
<td>Age:</td>
<td><?php echo $_POST['age']; ?></td>
</tr>
<tr>
<td>Address:</td>
<td><?php echo $_POST['address']; ?></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><?php echo $_POST['phone']; ?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php echo $_POST['email']; ?></td>
</tr>
</table>
</body>
</html>

You might also like