SESSION AND COOKIE IN PHP
first_session.php
<?php
session_start();
$_SESSION["name"] = "SK SAIF UDDIN";
$_SESSION["degree"] = "BCA";
echo $_SESSION["name"]."<br>";
echo $_SESSION["degree"]."<br>";
// session_unset();
// session_destroy();
echo $_SESSION["name"]."<br>";
?>
second_session.php
<?php
session_start();
echo "my name is ".$_SESSION['name'];
echo "<br>My degree is ".$_SESSION['degree'];
?>
third_session.php
<?php
session_start();
echo "My degree is ".$_SESSION['degree'];
?>
TASK 1====
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> </title>
</head>
<body>
<div class="container">
<form action="formaction.php" method="post">
<table>
<tr>
<th>Name:</th>
<td><input type="text" id="name" name="username"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="email" id="email" name="email"></td>
</tr>
<tr>
<th>Password:</th>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<th>Phone:</th>
<td><input type="number" id="number" name="phone"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="submit" value="submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
formaction.php
<?php
session_start();
$_SESSION['name'] = $_REQUEST['username'];
$_SESSION['email'] = $_REQUEST['email'];
$_SESSION['password'] = $_REQUEST['password'];
$_SESSION['phone'] = $_REQUEST['phone'];
echo "<p>My Name is: ". $_SESSION['name']."</p><br>";
echo "<p>My email is: ". $_SESSION['email']."</p><br>";
echo "<p>My password is: ". $_SESSION['password']."</p><br>";
echo "<p>My phone no is: ". $_SESSION['phone']."</p><br>";
?>
Third_form.php
<?php
session_start();
echo "this is the third form file<br>";
echo "<p>My Name is: ". $_SESSION['name']."</p><br>";
echo "<p>My email is: ". $_SESSION['email']."</p><br>";
echo "<p>My password is: ". $_SESSION['password']."</p><br>";
echo "<p>My phone no is: ". $_SESSION['phone']."</p><br>";
?>
Page view counter.php =
<?php
session_start();
if(isset($_SESSION['count'])){
$_SESSION['count'] = $_SESSION['count'] +1;
}
else{
$_SESSION['count'] = 1;
}
echo "Page Views: ".$_SESSION['count'];
// session_unset();
?>
COOKIES ====
check.php
<?php
// print_r($_POST['sub']);
if(isset($_REQUEST["sub"])){
$sub = $_REQUEST["sub"][0];
$subVal = $_REQUEST["sub"][1];
if(isset($_COOKIE['sub'])){
echo "Please wait 15 sec, Record inserted for ".$_COOKIE['sub'];
// echo time();
}else{
setcookie("sub",$sub,time()+10);
setcookie("val",$subVal,time()+10);
echo "You got ".$subVal." marks in ".$sub ;
}
}
?>
marks.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<select id="subjects" name="subj">
<option value="">Select</option>
<option value="SANSKRIT">SANSKRIT</option>
<option value="TAMIL">TAMIL</option>
<option value="HINDI">HINDI</option>
<option value="MATHS">MATHS</option>
<option value="SCIENCE">SCIENCE</option>
<option value="HISTORY">HISTORY</option>
</select><br><br><br>
</div>
<div id="col" style="visibility: hidden;">
<label>Enter Number:</label>
<input type="text" id="coldata" name='val'><br><br>
<input type="submit" name="submit" value="Submit" id="submit"><br><br>
<h4 id="result"></h4>
</div>
<script type="text/javascript">
$("document").ready(function(){
$("#subjects").change(function(){
var sub =[];
var subj= $("#subjects").val();
sub.push(subj);
$("#col").css("visibility","visible");
$("#submit").click(function (event){
var subjVal = $("#coldata").val();
sub.push(subjVal);
$.ajax({
method:"POST",
url:"check.php",
data: {sub:sub},
success: function(result){
$("#result").html(result);
}
});
});
});
});
</script>
</body>
</html>
Voting task.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> </title>
</head>
<body>
<form method="post">
<input type="submit" name="rate" value="PHP">
VS
<input type="submit" name="rate" value="ASP">
</form>
<?php
if(isset($_REQUEST["rate"])){
$R = $_REQUEST['rate'];
// echo $R;
if(isset($_COOKIE['rate1'])){
echo "Already votted in ".$_COOKIE['rate1'];
// echo time();
}else{
setcookie("rate1",$R,time()+10);
echo "Tanks for voting";
}
}
?>
</body>
</html>
Comments
Post a Comment