PHP is popular scripting language that most use in Web-development.
PHP is Used for website server side scripting you can learn PHP easily its simple and user friendly language.
In this article we learn What Is Session And Cookies And how To Use?
What is PHP Session and Cookies?
Session and cookies is use to save the information of user in server or Browser. many sites ask to use cookies means site want your saved data from your browser.
in short Session and Cookies save your data for later use like your Name , Email , Address , Mobile number etc.....
JavaScript Timer Enable button
PHP SESSION And COOKIES Small Project
In this project Collect information from user like NAME and PASSWORD. and we also collect Home page Background and font color.
example : http://aniljadav.rf.gd/session
for this project we create 3 different PHP file. first one is for LOGIN page , second is for HOME page and last one for SETTING background and font color.
Note : Php code in Red color , Html code in green color , Css code in black color
Header is use to redirect the user to another page.
#1 Login.php (collecting data)
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<style type="text/css">
body{ width: 100%;}
div{width:100%; display: flex; justify-content: center;}
input{padding:10px; margin:5px; border: 0.4px solid green; border-radius: 5px;}
</style>
</head>
<body>
<div>
<form method="GET">
<input type="text" name="user" placeholder="user name">
<input type="text" name="pass" placeholder="Password "><br>
<input type="submit" name="Submit" placeholder="LOGIN"><br>
<input type="checkbox" name="chk"> do you want custom background?<br>
choose font color:
<input type="color" name="FC" placeholder="font color"><br>
choose bg color :
<input type="color" name="BC" placeholder="back color "><br>
</form></div></body>
</html>
<?php
if(isset($_REQUEST["Submit"]))
{
$_SESSION["user"] = $_REQUEST["user"];
$_SESSION["pass"] = $_REQUEST["pass"];
if (isset($_REQUEST["chk"])) {
$_SESSION["bg"]=$_REQUEST["BC"];
$_SESSION["fc"]=$_REQUEST["FC"];
}
}
if(isset($_SESSION["user"]))
{
header("Location: home.php");
}
?>
#2 Home.php (output page)
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body{
background-color: <?php if(isset($_SESSION["bg"])){echo $_SESSION["bg"];} else{echo "silver";}?>;
color: <?php if(isset($_SESSION["fc"])){echo $_SESSION["fc"];} else{echo "black";}?>;
width: 100%;
margin: 0px;
padding: 0px;
}
form button{
float: right;
padding: 4px;
margin: 3px;
border: 1px solid <?php if(isset($_SESSION["fc"])){echo $_SESSION["fc"];} else{echo "black";}?>;
color: <?php if(isset($_SESSION["fc"])){echo $_SESSION["fc"];} else{echo "black";}?>;
border-radius: 4px;
background-color: <?php if(isset($_SESSION["bg"])){echo $_SESSION["bg"];} else{echo "silver";}?>;
}
form button:hover {
background-color: white;
color: black;
border:1px solid black;
}
div{
margin: 10px;
}
</style>
</head>
<body>
<div>
<form method="GET">
<b>Welcome <?php echo $_SESSION["user"]; ?> </b> <button name="niljadav">SOURCE CODE</button> <button name="set">SETTING</button> <button name="logout">LOGOUT</button>
</form>
</div>
</body>
</html>
<?php
if(isset($_SESSION["user"]))
{
}
else{ header("Location: index.php");}
if(isset($_REQUEST["logout"]))
{
session_unset();
session_destroy();
header("Location: index.php");
}
if (isset($_REQUEST["set"])) {
header("Location: settting.php");
}
if (isset($_REQUEST["niljadav"])) {
header("Location: https://www.niljadav.com");
}
?>
#3 setting.php (change Background and font color)
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>Custom bg</title>
<style type="text/css">
body{ width: 100%;}
div{width:100%; display: flex; justify-content: center;}
input{padding:10px; margin:5px; border: 0.4px solid green; border-radius: 5px;}
</style>
</head>
<body>
<div>
<form method="GET">
choose font color:
<input type="color" name="FC" placeholder="font color"><br>
choose bg color :
<input type="color" name="BC" placeholder="back color "><br>
<input type="submit" name="Submit" placeholder="LOGIN"><br>
</form>
</div>
</body>
</html>
<?php
if(isset($_REQUEST["Submit"]))
{
$_SESSION["bg"]=$_REQUEST["BC"];
$_SESSION["fc"]=$_REQUEST["FC"];
header("Location: http://localhost/webs/session/bg%20change%20session/home.php");
}
?>
</body>
</html>
All 3 files are ready and put all files in same folder. i hope this simple php project is helpfull for you.