What are some common security vulnerabilities in PHP and MySQL applications?

One common security vulnerability in PHP and MySQL applications is SQL injection, where attackers can manipulate SQL queries to access or modify data in the database. To prevent SQL injection, developers should use prepared statements with parameterized queries to sanitize user input.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
```

Another common vulnerability is cross-site scripting (XSS), where attackers inject malicious scripts into web pages viewed by other users. To prevent XSS, developers should properly escape and sanitize user input before displaying it on the webpage.

```php
// Example of escaping user input to prevent XSS
$escaped_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo $escaped_input;
```

Additionally, insecure file uploads can lead to vulnerabilities in PHP applications. Developers should validate file types, restrict file permissions, and store files outside the web root directory to prevent attackers from uploading malicious files.

```php
// Example of validating file uploads to prevent security vulnerabilities
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
$upload_path = '/path/to/uploads/';

if (in_array(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION), $allowed_extensions)) {
    move_uploaded_file($_FILES['file']['tmp_name'], $upload_path . $_FILES['file']['name']);
}