What are some potential security risks associated with allowing users to input custom URLs in PHP applications?

Allowing users to input custom URLs in PHP applications can pose security risks such as SQL injection, cross-site scripting (XSS) attacks, and directory traversal. To mitigate these risks, it is important to sanitize and validate user input before using it in any database queries or file operations.

// Sanitize and validate user input for custom URLs
$custom_url = filter_var($_GET['url'], FILTER_SANITIZE_URL);

// Use prepared statements for database queries to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM table WHERE url = :url");
$stmt->bindParam(':url', $custom_url);
$stmt->execute();

// Encode output to prevent XSS attacks
echo htmlspecialchars($custom_url);