How can one safely rewrite a login script with MySQL connection for a website hosted on Strato with register_globals OFF?
To safely rewrite a login script with MySQL connection for a website hosted on Strato with register_globals OFF, you should use PHP superglobals like $_POST or $_SESSION to retrieve and store user input data instead of relying on register_globals. This will help prevent security vulnerabilities such as injection attacks.
<?php
session_start();
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$_SESSION["loggedin"] = true;
header("Location: welcome.php");
} else {
echo "Invalid username or password";
}
}
$conn->close();
?>
Related Questions
- What are some common pitfalls when trying to display images from a SQL database in PHP?
- How can the use of header.html and footer.html files improve the efficiency of loading content in PHP templates?
- What are the best practices for storing openssl encoded data in a MySQL database using PHP PDO, considering compatibility issues between Windows and Linux?