如果您是 PHP 新手,并且想了解如何处理表单和使用会话数据,本指南适合您。我们将构建一个简单的项目,用户可以在其中提交表单、在会话中保存数据、查看数据以及稍后删除数据。在本教程结束时,您将了解 PHP 表单处理、验证、清理和会话的基础知识。
表单处理是指捕获通过 HTML 表单提交的数据并使用 PHP 等服务器端语言进行处理的过程。这包括:
会话是为单个用户跨多个页面存储信息(变量)的一种方式。与 cookie 不同,会话数据存储在服务器上,使其更加安全。要在 PHP 中开始使用会话,我们使用 session_start() 函数。
我们的项目涉及以下功能:
这是我们项目的结构:
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
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>
此文件会破坏会话数据。
<?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>
表格处理:
会议:
$_SESSION 数组。
该项目演示了 PHP 表单处理和会话管理的基础知识。利用这些概念,您可以构建更复杂的应用程序,安全有效地处理用户输入。试验代码,并尝试通过添加数据库集成或更高级的验证等功能来扩展它。
编码愉快! ?
以上是PHP 会话表单处理初学者指南的详细内容。更多信息请关注PHP中文网其他相关文章!