What best practices should be followed when transferring values between pages using PHP forms?
When transferring values between pages using PHP forms, it is best practice to use the POST method to securely pass data. This helps prevent sensitive information from being displayed in the URL. Additionally, it is important to validate and sanitize user input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks.
// Form on page1.php
<form action="page2.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Submit</button>
</form>
// page2.php
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
// Validate and sanitize input
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
// Use the sanitized values as needed
echo "Username: $username";
echo "Password: $password";