Skip to content

Commit bb6dba0

Browse files
committed
Renamed folders and files of 2_Intermediate folder
1 parent 7d40f00 commit bb6dba0

27 files changed

+802
-0
lines changed

2_Intermediate/10_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+
}

2_Intermediate/11_setcookie.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
echo "Welcome to the world of cookies<br>";
3+
echo time();
4+
setcookie("category", "Books", time() + 86400, "/");
5+
echo "The cookie is set<br>";
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+
}

2_Intermediate/13_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+
?>

2_Intermediate/1_getMethod.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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>Get Method</title>
8+
<style>
9+
form {
10+
margin: 20px;
11+
padding: 20px;
12+
border: 1px solid #ccc;
13+
border-radius: 5px;
14+
background-color: #f2f2f2;
15+
}
16+
17+
input[type="text"],
18+
input[type="number"] {
19+
margin-bottom: 10px;
20+
padding: 5px;
21+
width: 200px;
22+
border: 1px solid #ccc;
23+
border-radius: 3px;
24+
}
25+
26+
input[type="submit"] {
27+
padding: 5px 10px;
28+
background-color: #4CAF50;
29+
color: white;
30+
border: none;
31+
border-radius: 3px;
32+
cursor: pointer;
33+
}
34+
</style>
35+
</head>
36+
37+
<body>
38+
<form action="1_getMethod.php" method="get">
39+
<input type="text" name="name" placeholder="Enter your name">
40+
<input type="number" name="age" placeholder="Enter your age">
41+
<input type="submit" value="Submit">
42+
</form>
43+
<?php
44+
if (isset($_GET['name'])) {
45+
echo "Hello, " . $_GET['name'];
46+
} else if (isset($_GET['age'])) {
47+
echo "Your age is " . $_GET['age'];
48+
}
49+
?>
50+
</body>
51+
52+
</html>

2_Intermediate/2_postMethod.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>Post Method</title>
8+
<style>
9+
form {
10+
margin: 20px;
11+
padding: 20px;
12+
border: 1px solid #ccc;
13+
border-radius: 5px;
14+
background-color: #f2f2f2;
15+
}
16+
17+
input[type="text"],
18+
input[type="number"] {
19+
margin-bottom: 10px;
20+
padding: 5px;
21+
width: 200px;
22+
border: 1px solid #ccc;
23+
border-radius: 3px;
24+
}
25+
26+
input[type="submit"] {
27+
padding: 5px 10px;
28+
background-color: #4CAF50;
29+
color: white;
30+
border: none;
31+
border-radius: 3px;
32+
cursor: pointer;
33+
}
34+
</style>
35+
</head>
36+
37+
<body>
38+
<form action="2_postMethod.php" method="post">
39+
<input type="text" name="name" placeholder="Enter your name">
40+
<input type="number" name="age" placeholder="Enter your age">
41+
<input type="submit" value="Submit">
42+
</form>
43+
44+
<?php
45+
if (isset($_POST['name'])) {
46+
echo "Hello, " . $_POST['name'];
47+
} else if (isset($_POST['age'])) {
48+
echo "Your age is " . $_POST['age'];
49+
}
50+
?>
51+
</body>
52+
53+
</html>
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+
<form action="3_requestVariable.php" method="get">
12+
<input type="text" name="name" placeholder="Enter your name">
13+
<input type="number" name="age" placeholder="Enter your age">
14+
<input type="submit" value="Submit">
15+
</form>
16+
17+
<?php
18+
19+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
20+
echo "You have submitted the form using POST method";
21+
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
22+
echo "You have submitted the form using GET method";
23+
}
24+
?>
25+
</body>
26+
27+
</html>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Superglobals
2+
In PHP, superglobals are built-in global arrays that are available across all scopes (global, function, class) without the need for global declarations. They are automatically populated by the server with information, typically regarding server, environment, and user input data. These arrays are accessible from anywhere in the script.
3+
4+
<br/>
5+
6+
Here are the key PHP superglobals:
7+
8+
<br/>
9+
10+
1. `$_GET`: Contains variables passed to the script via the URL query string (i.e., after the ? in the URL).
11+
```php
12+
echo $_GET['name']; // URL: script.php?name=John
13+
```
14+
15+
16+
2. `$_POST`: Contains data passed to the script via an HTTP POST request (commonly from HTML forms).
17+
```php
18+
echo $_POST['username']; // Retrieved from an HTML form submission
19+
```
20+
21+
22+
3. `$_REQUEST`: A combination of $_GET, $_POST, and $_COOKIE data.
23+
```php
24+
echo $_REQUEST['email']; // Can retrieve from GET, POST, or cookies
25+
```
26+
27+
28+
4. `$_SERVER`: Contains information about headers, paths, and script locations.
29+
```php
30+
echo $_SERVER['HTTP_USER_AGENT']; // Shows browser information
31+
echo $_SERVER['REQUEST_METHOD']; // Shows the request method (GET, POST, etc.)
32+
```
33+
34+
35+
5. `$_FILES`: Contains information about files uploaded via an HTTP POST request.
36+
```php
37+
echo $_FILES['file']['name']; // Name of the uploaded file
38+
```
39+
40+
41+
6. `$_ENV`: Contains environment variables passed to the current script.
42+
```php
43+
echo $_ENV['HOME']; // Access an environment variable
44+
```
45+
46+
47+
7. `$_COOKIE`: Contains variables passed to the script via HTTP cookies.
48+
```php
49+
echo $_COOKIE['user']; // Retrieved from the user's browser cookies
50+
```
51+
52+
53+
8. `$_SESSION`: Used to store session variables for use across multiple pages.
54+
```php
55+
$_SESSION['user'] = 'John';
56+
```
57+
58+
9. `$GLOBALS`: It is a superglobal associative array. This superglobal can be used to access global variables from anywhere in the script. Takes variable names as the key.
59+
```php
60+
$x = 10;
61+
function test() {
62+
echo $GLOBALS['x']; // Accesses the global variable $x
63+
}
64+
```

2_Intermediate/5_validation.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
<style>
9+
.error {
10+
color: red;
11+
}
12+
</style>
13+
</head>
14+
15+
<body>
16+
<?php
17+
// Define variables and set to empty values
18+
$nameErr = $emailErr = $genderErr = $websiteErr = "";
19+
$name = $email = $gender = $comment = $website = "";
20+
21+
// Check if the form was submitted
22+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
23+
24+
// Validate name
25+
if (empty($_POST["name"])) {
26+
$nameErr = "Name is required";
27+
} else {
28+
$name = test_input($_POST["name"]);
29+
}
30+
31+
// Validate email
32+
if (empty($_POST["email"])) {
33+
$emailErr = "Email is required";
34+
} else {
35+
$email = test_input($_POST["email"]);
36+
// Check if email is valid
37+
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
38+
$emailErr = "Invalid email format";
39+
}
40+
}
41+
42+
// Validate website
43+
if (empty($_POST["website"])) {
44+
$website = ""; // Optional field
45+
} else {
46+
$website = test_input($_POST["website"]);
47+
// Check if website URL is valid
48+
if (!filter_var($website, FILTER_VALIDATE_URL)) {
49+
$websiteErr = "Invalid URL format";
50+
}
51+
}
52+
53+
// Validate comment (optional field)
54+
if (!empty($_POST["comment"])) {
55+
$comment = test_input($_POST["comment"]);
56+
}
57+
58+
// Validate gender
59+
if (empty($_POST["gender"])) {
60+
$genderErr = "Gender is required";
61+
} else {
62+
$gender = test_input($_POST["gender"]);
63+
}
64+
}
65+
66+
// Function to sanitize form input data
67+
function test_input($data)
68+
{
69+
$data = trim($data); // Remove whitespace
70+
$data = stripslashes($data); // Remove slashes
71+
$data = htmlspecialchars($data); // Convert special characters
72+
return $data;
73+
}
74+
?>
75+
<h2>PHP Form Validation</h2>
76+
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
77+
Name: <input type="text" name="name" value="<?php echo $name; ?>">
78+
<span class="error">* <?php echo $nameErr; ?></span>
79+
<br><br>
80+
E-mail: <input type="text" name="email" value="<?php echo $email; ?>">
81+
<span class="error">* <?php echo $emailErr; ?></span>
82+
<br><br>
83+
Website: <input type="text" name="website" value="<?php echo $website; ?>">
84+
<span class="error"><?php echo $websiteErr; ?></span>
85+
<br><br>
86+
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment; ?></textarea>
87+
<br><br>
88+
<input type="submit" value="Submit" name="submit">
89+
90+
<?php
91+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
92+
echo "<h2>Your Input:</h2>";
93+
echo "Name: " . $name . "<br>";
94+
echo "Email: " . $email . "<br>";
95+
echo "Website: " . $website . "<br>";
96+
echo "Comment: " . $comment . "<br>";
97+
}
98+
?>
99+
</body>
100+
101+
</html>

0 commit comments

Comments
 (0)