Example : How to Pass Single value from a web page to another using session in php?
//Page1.php (from where session value arise)
<?php
error_reporting(E_ERROR | E_PARSE);
$sn3=$_REQUEST['sn1'];
session_start();
$_SESSION['s1']=$sn3;
$_SESSION['s2']="Codershelpline";
$_SESSION['s3']=15;
//echo $_SESSION['s1'];
//echo $_SESSION['s2'];
//echo $_SESSION['s3'];
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" id="form2" method="post" action="">
<center>
<br><br>
<fieldset style="width: 700px;">
<table>
<tr>
<td> Enter Dynamic Value </td>
<td> : </td>
<td><input type="text" name="sn1" id="sn2" value=""></td>
</tr>
<tr>
<td></td>
<td></td>
<td> <input type="Submit" name="sub1" id="sub2" value="Submit">
<input type="Submit" name="res1" id="res2" value="Reset">
</td>
</tr>
</table>
</fieldset>
</center>
</form>
</body>
</html>
//Page2.php (where session value used)
<?php
session_start();
$value1=$_SESSION['s1'];
$value2=$_SESSION['s2'];
$value3=$_SESSION['s3'];
//echo $value1;
//echo $value2;
//echo $value3;
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" id="form2" method="post" action="">
<center>
<br><br>
<fieldset style="width: 700px;">
<table>
<tr>
<td> Dynamic Value </td>
<td> : </td>
<td><input type="text" name="n1" id="n2" value="<?php echo $value1;?>"></td>
</tr>
<tr>
<td> String Static Value </td>
<td> : </td>
<td><input type="text" name="sn1" id="sn2" value="<?php echo $value2;?>"></td>
</tr>
<tr>
<td> Number Static Value </td>
<td> : </td>
<td><input type="text" name="f1" id="f2" value="<?php echo $value3; ?>"></td>
</tr>
<tr>
<td></td>
<td></td>
<td> <input type="Submit" name="sub1" id="sub2" value="Submit">
<input type="Submit" name="res1" id="res2" value="Reset">
</td>
</tr>
</table>
</fieldset>
</center>
</form>
</body>
</html>
Example : How to Pass Multiple values from a web page to another using Session Array in php?
Page1.php
<?php
error_reporting(E_ERROR | E_PARSE);
$testTxtuname3=$_REQUEST['testTxtuname1'];
$testPhnumber3=$_REQUEST['testPhnumber1'];
$testTxtemail3=$_REQUEST['testTxtemail1'];
session_start();
$_SESSION['S1']=array(); //Array declaration for session
//Values Assignment in Array Session
array_push($_SESSION['S1'],$testTxtuname3,$testPhnumber3,$testTxtemail3);
// OR
$_SESSION['S1'][0]=$testTxtuname3;
$_SESSION['S1'][1]=$testPhnumber3;
$_SESSION['S1'][2]=$testTxtemail3;
// To Print All Array Session Values, if required
foreach($_SESSION['S1'] as $varname)
{
echo $varname;
}
// To Print customized Array Session Value, if required
echo $_SESSION['S1'][0];
echo $_SESSION['S1'][1];
echo $_SESSION['S1'][2];
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="testpage1" method="post" action="">
<table>
<tr>
<td>User Name:-</td>
<td><input type="text" name="testTxtuname1" id="testTxtuname2"></td>
</tr>
<tr>
<td>Phone no:-</td>
<td><input type="number" name="testPhnumber1" id="testPhnumber2"></td>
</tr>
<tr>
<td>Email:-</td>
<td><input type="Email" name="testTxtemail1" id="testTxtemail2"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="sub1" id="sub2" value="Submit">
<input type="submit" name="res1" id="res2" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
Page2.php
<?php
error_reporting(E_ERROR | E_PARSE);
session_start();
$PageTxtuname3=$_SESSION['S1'][0];
$PagePhnumber3=$_SESSION['S1'][1];
$PageTxtemail3=$_SESSION['S1'][2];
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="Page1" id="page2" method="post" action="">
<table>
<tr>
<td>User Name:-</td>
<td><input type="text" name="PageTxtuname1" id="PageTxtuname2" value="<?php echo $PageTxtuname3;?>"></td>
</tr>
<tr>
<td>Phone no:-</td>
<td><input type="number" name="Pagepnumber1" id="Pagepnumber2" value="<?php echo $PagePhnumber3;?>"></td>
</tr>
<tr>
<td>Email:-</td>
<td><input type="Email" name="PageTxtemail1" id="PageTxtemail2" value="<?php echo $PageTxtemail3;?>"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="sub1" id="sub2" value="Submit">
<input type="submit" name="res1" id="res2" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
Example : How to Login and Logout using session in php?
---------------------------------------------------
Page1.php
---------------------------------------------------
<?php
error_reporting(E_ERROR | E_PARSE);
$TxtUName3=$_REQUEST['TxtUName1'];
$TxtPass3=$_REQUEST['TxtPass1'];
$TxtUName4="Codershelpline";
$TxtPass4="1234";
if(isset($_REQUEST['sub1'])){
if($TxtUName4==$TxtUName3 && $TxtPass4==$TxtPass3){
session_start();
$_SESSION['s1']=$TxtUName3;
header('location: login2.php ');
}
else {
echo ("Worng Password...! Please Enter Valid Data...! ");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>A typical Bootstrap Form Template</title>
<!-- Bootstrap CSS 4.5.2 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
label{
color:blue;
}
</style>
</head>
<body>
<!-- Normal Input Fields -->
<section>
<div class="container my-4">
<div class="card">
<div class="card-header">
<h2>Log-In</h2>
</div>
<div class="card-body">
<form action="" method="post" name="form1" id="form2" enctype="multipart/form-data">
<div class="form-group">
<label for="TxtUName">User Name</label>
<input type="text" class="form-control" name="TxtUName1" id="TxtUName2" aria-describedb="TxtNameHelp"/>
<small id="TxtNameHelp" class="form-text text-muted">Enter Your Correct User Name</small>
</div>
<div class="form-group">
<label for="TxtPass">Password</label>
<input type="password" class="form-control" name="TxtPass1" id="TxtPass2" aria-describedby="TxtPassHelp"/>
<small id="TxtPassHelp" class="form-text text-muted">Enter Your Correct Password</small>
</div>
<div class="form-group">
<button type="submit" name="sub1" class="btn btn-success" >Log-In </button>
<input type="reset" class="btn btn-info" value="Reset"/>
</div>
</form>
</div>
</div>
</div>
</section>
<!-- Bootstrap JS, Popper.js, and jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
---------------------------------------------------
Page2.php
---------------------------------------------------
<?php
error_reporting(E_ERROR | E_PARSE);
$TxtUName3=$_REQUEST['TxtUName1'];
$TxtPass3=$_REQUEST['TxtPass1'];
session_start();
$value1=$_SESSION['s1'];
if(isset($_REQUEST['sub1'])){
session_destroy();
echo "<script>alert('Log Out Susscess Fully..!')</script>";
echo "<script>window.location.href = 'login.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>A typical Bootstrap Form Template</title>
<!-- Bootstrap CSS 4.5.2 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
label{
color:blue;
}
</style>
</head>
<body>
<!-- Normal Input Fields -->
<section>
<div class="container my-4">
<div class="card">
<div class="card-header">
<h2>Welcome <?php echo $value1; ?></h2>
</div>
<div class="card-body">
<form action="" method="post" name="form1" id="form2" enctype="multipart/form-data">
<div class="form-group">
<label for="TxtUName"> Login Susscess..</label> <br>
<?php echo $value1; ?> Now, You are on Logged Page..
</div>
<div class="form-group">
<button type="submit" name="sub1" class="btn btn-success" >Log-Out </button>
<input type="reset" class="btn btn-info" value="Reset"/>
</div>
</form>
</div>
</div>
</div>
</section>
<!-- Bootstrap JS, Popper.js, and jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
0 Comments