How can a PHP script be modified to allow users to log in with a specific code instead of a username and password?
To allow users to log in with a specific code instead of a username and password, you can modify the login script to check the input code against a predefined value. This can be done by creating a form with a single input field for the code and then comparing the input code with the predefined code in the PHP script.
<?php
// Predefined code for login
$valid_code = "12345";
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input_code = $_POST['code'];
// Check if the input code matches the valid code
if ($input_code == $valid_code) {
// Code is correct, allow access
echo "Login successful!";
} else {
// Code is incorrect, show error message
echo "Invalid code. Please try again.";
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="code">Enter Code:</label>
<input type="text" id="code" name="code" required>
<button type="submit">Login</button>
</form>