
PHP? ?? ???? ?? ?? ? ?? ??? ?? ??? ??? ??? ? ???? ?????. ???? ??? ????, ??? ???? ????, ??, ??? ??? ? ?? ??? ????? ???????. ? ????? ??? PHP ?? ??, ??? ??, ?? ? ??? ?? ??? ???? ???.
PHP? ?? ??? ??????
?? ??? HTML ??? ?? ??? ???? ???? PHP? ?? ??? ??? ???? ??? ?????. ???? ??? ?????.
-
?? ??? ????: GET ?? POST? ?? ?? ??
-
??? ??? ??: ??? ?? ??? ????? ?????.
-
??? ??: XSS ??? ?? ?? ??? ???? ?? ??? ?????.
-
??? ?? ?? ??: ??, ??????? ????? ????? ?? ?????.
PHP? ???? ??????
??? ?? ???? ?? ?? ???? ?? ??(??)? ???? ?????. ??? ?? ?? ???? ??? ????? ?? ?????. PHP?? ?? ??? ????? session_start() ??? ?????.
????: ?? ??? ??? PHP ??
?? ?????? ?? ??? ?????.
- ???? ????? ???? ??? ?????.
- ??? ???? ?? ? ?????.
- ???? ??? ???? ?????.
- ???? ?? ???? ??? ??? ? ????.
?? ??
?? ????? ??? ??? ????.
project-folder/
│
├── index.php # Form page
├── submit.php # Form handling and session storage
├── view_session.php # Viewing session data
├── delete_session.php # Deleting session data
1??: ?? ???(index.php)
index.php ???? ???? ?? ??? ??? ? ?? ??? HTML ??? ???? ????. ??? ??? ????.
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Form with Session Handling</title>
</head>
<body>
<h1>Submit Your Information</h1>
<!-- Form Section for User Input -->
<form method="get" action="submit.php">
<label for="name">Name:</label><br>
<input type="text">
<hr>
<h3>
Step 2: Handling Form Submission (submit.php)
</h3>
<p>This file processes the submitted form data, validates it, and stores it in a session. Here's the code:<br>
</p>
<pre class="brush:php;toolbar:false"><?php
session_start();
// Initialize error messages and data variables
$error_name = "";
$error_age = "";
$error_email = "";
$error_website = "";
$name = $age = $email = $website = $gender = $comments = $hobbies = "";
// Sanitize and validate the form data
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// Sanitize inputs
$name = htmlspecialchars(trim($_GET['name']));
$age = htmlspecialchars(trim($_GET['age']));
$email = htmlspecialchars(trim($_GET['email']));
$website = htmlspecialchars(trim($_GET['website']));
$gender = isset($_GET['gender']) ? $_GET['gender'] : 'No gender selected.';
$hobbies = isset($_GET['hobbies']) ? $_GET['hobbies'] : ['No hobby selected.'];
$comments = htmlspecialchars(trim($_GET['comments']));
// Validation checks
if (empty($name)) {
$error_name = "Name is required.";
}
if (empty($age) || !filter_var($age, FILTER_VALIDATE_INT) || $age <= 0) {
$error_age = "Valid age is required.";
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_email = "Valid email is required.";
}
if (empty($website) || !filter_var($website, FILTER_VALIDATE_URL)) {
$error_website = "Valid website URL is required.";
}
// If no errors, store data in session
if (empty($error_name) && empty($error_age) && empty($error_email) && empty($error_website)) {
// Store data in session
$_SESSION['name'] = $name;
$_SESSION['age'] = $age;
$_SESSION['email'] = $email;
$_SESSION['website'] = $website;
$_SESSION['gender'] = $gender;
$_SESSION['hobbies'] = implode(", ", $hobbies);
$_SESSION['comments'] = $comments;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Submission Result</title>
</head>
<body>
<h1>Form Submission Result</h1>
<!-- Show Errors if any -->
<?php
if ($error_name) {
echo "<p>
<hr>
<h3>
Step 3: Viewing Session Data (view_session.php)
</h3>
<p>This file displays session data stored on the server.<br>
</p>
<pre class="brush:php;toolbar:false"><?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Session Data</title>
</head>
<body>
<h1>View Stored Session Data</h1>
<?php
if (isset($_SESSION['name'])) {
echo "<p><strong>Name:</strong> " . $_SESSION['name'] . "</p>";
echo "<p><strong>Age:</strong> " . $_SESSION['age'] . "</p>";
echo "<p><strong>Email:</strong> " . $_SESSION['email'] . "</p>";
echo "<p><strong>Website:</strong> <a href='" . $_SESSION['website'] . "' target='_blank'>" . $_SESSION['website'] . "</a></p>";
echo "<p><strong>Gender:</strong> " . $_SESSION['gender'] . "</p>";
echo "<p><strong>Hobbies:</strong> " . $_SESSION['hobbies'] . "</p>";
echo "<p><strong>Comments:</strong> " . $_SESSION['comments'] . "</p>";
} else {
echo "<p>No session data found!</p>";
}
?>
<br><br>
<a href="index.php">Go Back</a>
</body>
</html>
4??: ?? ??? ??(delete_session.php)
? ??? ?? ???? ?????.
<?php
session_start();
session_unset(); // Remove all session variables
session_destroy(); // Destroy the session
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Session Deleted</title>
</head>
<body>
<h1>Session Data Deleted</h1>
<p>All session data has been deleted successfully.</p>
<br><br>
<a href="index.php">Go Back</a>
</body>
</html>
??? ?? ??
-
?? ??:
- GET ???? ???? ???? ?????.
- ?? ? ??? ???? ?? ???? ?????.
- htmlspecialchars()? ??? ?????.
-
??:
- session_start()? ?? ??.
- ??? ??
$_SESSION ??.
- ?? ??? ??.
- session_unset() ? session_destroy()? ???? ?? ???? ?????.
??
? ????? PHP ?? ?? ? ?? ??? ?? ??? ?????. ??? ??? ???? ??? ??? ???? ????? ???? ?? ??? ??????? ??? ? ????. ??? ???? ?????? ???? ?? ??? ?? ??? ???? ??? ??? ???.
??? ?????! ?
? ??? ??? ??? PHP ?? ?? ??? ???? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!