PHP
PHP
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
if(isset($_COOKIE[$cookie_name])) {
$last_visited = $_COOKIE[$cookie_name];
} else {
?>
<!DOCTYPE html>
<html >
<head>
</head>
<body>
</body>
</html>
2. Write a program to demonstrate session.
Create_session.php
<?php
// Start the session
session_start();
<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();
<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();
<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>