Iicsa Php Lab
Iicsa Php Lab
AIM:
To create a PHP program that demonstrates the use of controls (such as loops,
conditionals) and user- defined functions.
ALGORITHM:
<html>
<head>
</head>
<body>
<h4>Factorial of a Given Number</h4>
<?php
if(isset($_POST['submit'])){
function fact($n){
if($n <= 1){
return 1;
}
else{
return $n * fact($n - 1);
}
}
$n =$_POST['number'];
$f = fact($n);
echo "Factorial of $n is $f";
}
?>
<form method="post">
<p>Enter the Number </p>
<input type="text" name="number" required>
<input type="submit" name="submit">
</form>
</body>
</html>
EX.NO:2 Develop a PHP program and check message passing mechanism between
pages.
AIM
To create a PHP program that passes messages between different pages using
sessions.
ALGORITHMS:
Create an HTML form that collects user input (e.g., name, email, or any
In the PHP script that receives the form submission, validate the
variable.
Form.html
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<h1>Simple Form</h1>
<form action="process_form.php" method="post">
Name:<input type="text" name="name"><br><br>
Email:<input type="email" id="email" name="email"><br><br>
Message:<textarea id="message" name="message" rows="5"
cols="30"></textarea><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
process_form.php:
<html>
<head>
<title>Form Data</title>
</head>
<body>
<h1>Form Data</h1>
<?php
echo "Name: " . $_POST['name'] . "<br>";
echo "Email: " . $_POST['email'] . "<br>";
echo "Message: " . $_POST['message'] . "<br>";
?>
<form action="form.php">
<input type="submit" value="message again">
</form>
</body>
</html>
EX.NO:3 Develop a PHP program using String function and Arrays.
AIM:
To create a PHP program that demonstrates the use of string functions and arrays.
ALGORITHM:
SETP 1: Input:
Prompt the user to enter a string (e.g., their name or any other text).
Display the transformed string (uppercase, length, and reversed) along with the
array of characters.
SOURCE CODE:
<html>
<html>
<head>
<?php
AIM:
To create a PHP program to display student information using MYSQL table
ALGORITHM:
Inside the database, create a table named students with the following
records.
<html>
<head>
<title>Student Information Form</title>
</head>
<body>
<form method="post">
Name of the student: <input type="text" name="full_name"><br><br>
Email ID: <input type="text" name="email"><br><br>
Phone Number: <input type="text" name="phone_number"><br><br>
<input type="submit" name="OK" value="Submit">
<input type="submit" name="DISPLAY" value="Display Data">
</form>
<?php
$con = mysqli_connect("localhost", "root", "", "student");
if (isset($_POST['OK']))
{
$full_name = mysqli_real_escape_string($con, $_POST['full_name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$phone_number = mysqli_real_escape_string($con, $_POST['phone_number']);
$sql = $con->prepare("INSERT INTO `college` (`full_name`, `email`, `phone_number`)
VALUES (?, ?, ?)");
$sql->bind_param("sss", $full_name, $email, $phone_number);
$sql->execute();
echo "Data has been successfully submitted!";
}
if (isset($_POST['DISPLAY'])) {
$result = mysqli_query($con, "SELECT * FROM `college`");
if ($result)
{
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Email</th><th>Phone Number</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row['full_name'] . "</td><td>" . $row['email'] . "</td><td>" .
$row['phone_number'] . "</td></tr>";
}
echo "</table>";
} else
{
echo "No data found.";
}
}
?>
</body>
</html>
EX.NO: 5 Develop a PHP program to design a college application form using
MYSQL table.
AIM:
ALGORITHM:
address)
Include input fields for full name, email, phone number, and other relevant
checkboxes, etc.).
is valid).
STEP: 4 Insert Data into MySQL Table:
college_applications table.
<html>
<head>
<style>
div {
align: center; width: 400px; height: 300px; border: solid black;
background-color: orange; padding: 20px;
}
</style>
</head>
<body>
<div>
<h1>COLLEGE APPLICATION FORM </h1>
<form method="post">
Name: <input type="text" name="name"><br><br>
Roll Number: <input type="text" name="rollNumber"><br><br><br>
EMailid:<input type="text" name="mailid"><br><br>
<input type="submit" name="OK">
</form>
<?php
$con = mysqli_connect("localhost", "root", "", "student");
if (isset($_POST['OK']))
{
$name = $_POST['name'];
$rollNumber = $_POST['rollNumber'];
$mailid = $_POST['mailid'];
AIM:
Ensure that the PHP Tokenizer extension is enabled in your PHP configuration
(usually found in php.ini). Remove the semicolon (;) from the line that starts
with extension=tokenizer.
Write or obtain the PHP code that you want to tokenize and parse. This can be
any valid PHP code snippet.
This function takes the input code as a string and returns an array of tokens.
Each token can be a single character or an array with three elements: the token
ID, the token text, and the line number where the token appears in the code.
You can use the token_name function to convert the token ID to a human-
readable string.
STEP: 5 Display or Manipulate the Tokens:
sourcecode
<?php
$code = 'echo "Hello, world!";';
$tokens = token_get_all($code);
ALGORITHM:
STEP: 1 Regular Expressions (Regex):
Define the specific patterns you want to search for (e.g., email addresses,
phone numbers, etc.).
You can also use loops and conditionals to create dynamic HTML structures.
Hashing functions are used to convert data (such as passwords) into fixed-
length hash values.
Create a PHP file that includes regular expressions, HTML functions, and
hashing functions.
<?php
$email = 'user@example.com';
if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email))
{
echo "Valid email address: $email<br><br>";
} else {
echo "Invalid email address<br>";
}
$password = 'my_secure_password';
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
echo "Hashed password: $hashedPassword<br><br><br>";
?>
EX.NO: 8. Develop a PHP program and check File System functions, Network
functions, Date and time functions.
AIM:
To develop a PHP program and check File System functions, Network functions, Date
and time functions.
ALGORITHM:
STEP: 1 File System Functions:
File system functions allow you to interact with files and directories on the
server.
Create, move, and manipulate directories using functions like mkdir, rmdir, and
scandir.
Use functions like file_get_contents (for fetching data from URLs), curl (for more
advanced HTTP requests), and fsockopen (for socket connections).
Use functions like date, strtotime, time, and strftime to format, parse, and
manipulate dates and times.
Create a PHP file that includes examples of file system operations, network
requests, and date/time manipulations.
$file = 'example.txt';
if (file_exists($file))
{
echo "The file '$file' exists.<br>";
}
else
{
echo "The file '$file' does not exist.<br>";
}
if (is_file($file))
{
echo "'$file' is a regular file.<br>";
}
else
{
echo "'$file' is not a regular file.<br>";
}
$dir = 'exampleDir';
if (is_dir($dir))
{
echo "'$dir' is a directory.<br>";
}
else
{
echo "'$dir' is not a directory.<br>";
}
if (file_exists($file))
{
echo "The size of '$file' is " . filesize($file) . " bytes.<br>";
}
else
{
echo "File '$file' does not exist to get its size.<br>";
}
echo "The current date and time is: " . date('Y-m-d H:i:s') . "<br>";
?>
EX.NO: 9. Develop a PHP program using
session.
AIM:
ALGORITHM:
$_SESSION['username'];.
When the user logs out or their session expires, call $_SESSION['username']
Source Code:
start_session
<?php
session_start();
$_SESSION['username'] = 'Tamil';
display_session.php
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Welcome, " . $_SESSION['username'] . "!";
} else {
echo "Session variable 'username' is not set.";
}
?>
EX.NO:10 Develop a PHP program using cookie and
session. AIM:
session. ALGORITHM:
STEP: 1 Cookies:
requests.
Use the setcookie function with the desired cookie name, value, and optional
parameters (such as expiration time, path, and domain).
STEP: 3 Sessions:
A session is unique for each user and lasts until the user closes the browser or
logs out.
as needed.