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

PHP Program

Uploaded by

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

PHP Program

Uploaded by

rahul Gupte
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

 PHP program to print sum of digits

 PHP program to check prime number


 PHP program to print table of a number
 PHP program to sort elements in an array in ascending order
 PHP program to sort elements in an array in descending order
 PHP program to print each element of an array
 PHP program to find number of elements in an array

PHP program to create Login and Logout using Sessions:


The below program is to create login and logout using PHP SESSIONS.

// Form.php
<!DOCTYPE html>
<html>
<body>
 
<form method="post" action="login.php">
User Id: <input type="text" name="userid"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
 
</body>
</html>
// Login.PHP
<!DOCTYPE html>
<html>
<body>
 
<?php
$uid = $_POST['userid'];
$pw = $_POST['password'];
 
if($uid == 'ben' and $pw == 'ben23')
{
session_start();
$_SESSION['sid']=session_id();
echo "Logged in successfully";
}
?>
 
</body>
</html>
// Logout.PHP
<!DOCTYPE html>
<html>
<body>
 
<?php
 
echo "Logged out successfully";
 
session_start();
session_destroy();
 
?>
 
</body>
</html>

Passing values between the pages


The session is an activity period where visitor's data is stored and passed to
following pages. We tell the PHP interpreter to start a session by
defining session_start() at the beginning of every PHP file we want session to
happen. Then we access the session variables using the $_SESSION['variable-
name'] method.

Example 1: PHP code to pass values between the pages


Session.php
<?php session_start();
//Put session start at the beginning of the file
?>

<!DOCTYPE html>
<html>
<head>
<title>Session Example</title>
</head>
<body>

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {


$_SESSION['name'] = $_POST['name'];

if($_SESSION['name']) {
header('location: printName.php');
}
}
?>

<form action="session.php" method="POST">


<input type="text" name="Name">
<input type="submit" value="submit">
</form>

</body>
</html>

Explanation
In this example, we are taking a text input in the form of name and storing it in the
name session variable. Note, this is the how the session variables are
defined, $_SESSION['name']

Next, note that we have included session_start() at the beginning of each PHP file.
This will ensure that we can safely access the variable defined in other page, by just
using $_SESSION['name'].

Example 2: PHP code to pass values between the pages

printName.php
<?php session_start();
//Put session start at the beginning of the file
?>

<!DOCTYPE html>
<html>
<head>
<title>Print Name</title>
</head>
<body>

<p>Your Name is: <?php echo $_SESSION['name']; ?></p>

</body>
</html>

Explanation
In printName.php file, echoing the session name variable prints the name we have
inputted from user in another page.

PHP code to create class's object, access its attributes


The source code to create an object of a class and access the class attributes is
given below. The given program is compiled and executed successfully.

<?php
//PHP program to create an object of a class and access class
attributes.
class Student
{
//Attributes
public $id;
public $name;
public $per;
}

$S = new Student();

$S->id = 101;
$S->name = "Rohit Kohli";
$S->per = 78.23;

print ("Student Id : " . $S->id . '<br>');


print ("Student Name : " . $S->name . '<br>');
print ("Student Percentage : " . $S->per . '<br>');
?>

Output
Student Id : 101
Student Name : Rohit Kohli
Student Percentage : 78.23

You might also like