Skip to content

Commit 78e41a1

Browse files
committed
Added codes 🚀
1 parent feb9501 commit 78e41a1

File tree

11 files changed

+127
-0
lines changed

11 files changed

+127
-0
lines changed

Intermediate/6_cookies.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<link rel="shortcut icon" href="../favicon.png" type="image/x-icon">
99
</head>
1010

11+
<!-- Small files stored on user's browser -->
12+
1113
<body>
1214
<h1 style="text-align: center;">Cookies</h1>
1315
<?php

Intermediate/checkcookie.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
setcookie("test_cookie", "test", time() + 5);
3+
if (count($_COOKIE) > 0) {
4+
echo "Cookies are enabled.";
5+
} else {
6+
echo "Cookies are disabled.";
7+
}

Intermediate/cookieform.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<form action="" method="POST">
2+
<input type="text" name="user">
3+
<input type="submit" name="set" value="set">
4+
<input type="submit" name="del" value="del">
5+
6+
</form>
7+
8+
<?php
9+
10+
if (isset($_POST['set'])) {
11+
12+
$cookie1 = $_POST['user'];
13+
setcookie($cookie1, $cookie1, time() + 3600);
14+
15+
?>
16+
17+
<?php
18+
if (isset($_COOKIE[$cookie1])) {
19+
echo "Cookie '" . $cookie1 . "' has been set<br>";
20+
echo "Value is: " . $_COOKIE[$cookie1];
21+
} else {
22+
echo "Cookie '" . $cookie1 . "' is not set";
23+
}
24+
}
25+
?>
26+
27+
<?php
28+
29+
if (isset($_POST['del'])) {
30+
$cookie1 = $_POST['user'];
31+
setcookie($cookie1, "", time() - 3600);
32+
echo $_COOKIE[$cookie1] . " has been deleted";
33+
}
34+
35+
?>

Intermediate/createcookie1.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
$cookie_name = "user";
3+
$cookie_value = "nav kaur";
4+
setcookie($cookie_name, $cookie_value, time() + 5); // 86400 = 1 day
5+
6+
if (isset($_COOKIE[$cookie_name])) {
7+
echo "Cookie '" . $cookie_name . "' has been set<br>";
8+
echo "Value is: " . $_COOKIE[$cookie_name];
9+
} else {
10+
echo "Cookie '" . $cookie_name . "' is not set";
11+
}

Intermediate/deletecookie.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
// set the expiration date to one hour ago
3+
setcookie("user", "", time() - 5);
4+
echo "Cookie 'user' is deleted.";

Intermediate/modifycookie.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
$cookie_name = "user";
3+
$cookie_value = "Vansh Chaurasiya";
4+
setcookie($cookie_name, $cookie_value, time() + 5);
5+
6+
if (isset($_COOKIE[$cookie_name])) {
7+
echo "Cookie '" . $cookie_name . "' has been set<br>";
8+
echo "Value is: " . $_COOKIE[$cookie_name];
9+
} else {
10+
echo "Cookie '" . $cookie_name . "' is not set";
11+
}

Intermediate/sessions/logout.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
session_start();
3+
session_unset();
4+
session_destroy();
5+
echo "<br> You have been logged out!";
6+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- It is used to manage information across different -->
2+
<!-- Stores data on server rather than user's computer -->
3+
<!-- Everytime we login a sid is created (session) -->
4+
<!-- A session is a way to store info (in variables) to be stored on diff web pages -->
5+
6+
<?php
7+
session_start();
8+
if(isset($_SESSION["username"])){
9+
echo "Welcome".$_SESSION["username"];
10+
echo "<br> Your favourite category is ".$_SESSION["category"];
11+
echo "<br>";
12+
}
13+
?>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
session_start();
3+
$_SESSION["username"]="Shashank";
4+
$_SESSION["category"]="Basketball";
5+
echo "We have saved your session";
6+
?>

Intermediate/sessions/sessions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
10+
<body>
11+
<?php
12+
session_start();
13+
$_SESSION["username"] = "Vansh";
14+
echo "Welcome " . $_SESSION["username"];
15+
?>
16+
</body>
17+
18+
</html>
19+
20+
<!--
21+
1. Used to manage
22+
2. In PHP, sessions allow you to store information in variables to be used across multiple pages, unlike cookies,
23+
session data is stored on the server reducing security risks
24+
3. starting a session: To start a session in PHP, use `session_start()` at the beginning of the script
25+
4. to destroy the session entirely, we use session_destroy() function
26+
5. once the session is set, they can be accessed by other pages as well until the session is destroyed or expired
27+
-->

0 commit comments

Comments
 (0)