Displaying news to the user based on the users preference using a cookie
maincookie.php (Page that display the news from the database)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to our News Channel</title>
</head>
<body>
<label for="interests">Register your Interest</label> <br>
<a href="cookie.php">Register Here</a>
</body>
</html>
<?php
include "db.php";
$conn = OpenCon();
if(isset($_COOKIE['interest'])&& $_COOKIE['interest']!=null)
{
$interest = $_COOKIE['interest'];
$stmt = $conn->prepare("select * from news where category=?;");
$stmt->bind_param('s',$interest);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0)
{
// output data of each row
echo "<table border='1'>
<tr>
<th>Topic</th>
<th>Category</th>
<th>Content</th>
</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['topic'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Sorry - We have no News";
}
}else
{
$stmt = $conn->prepare("select * from news");
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0)
{
// output data of each row
echo "<table border='1'>
<tr>
<th>Topic</th>
<th>Category</th>
<th>Content</th>
</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['topic'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Sorry - We have no News";
}
}
CloseCon($conn);
?>
db.php (Database Connection Code)
<?php
#Establishing a connection with the database
function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$db = "satish";
$conn = new mysqli($dbhost, $dbuser,"",$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
cookie.php - sets the cookie by getting the interest from the user.
<!--Demonstrate the use of cookies in PHP-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cookies Demo in PHP</title>
</head>
<body>
<form action="cookie.php" method="post" onsubmit="return true;">
<label for="topics">Select a topic of Interest</label> <br>
<input type="radio" name="interest" value="music">Music <br>
<input type="radio" name="interest" value="sports">Sports <br>
<input type="radio" name="interest" value="automobile">Automobile <br>
<input type="radio" name="interest" value="crafts">Crafts <br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
setcookie('interest',$_POST['interest'],time() + 2 * 24 * 60 * 60);
echo "your interest registered. Thanks";
}
?>
</body>
</html>
Deleting a Cookie
<?php
setcookie('interest','',time()-3600);
echo "cookie is deleted";
?>
Output
Enter the Program title here
copy and paste the code here
Output