Skip to content

Instantly share code, notes, and snippets.

@primitiveshaun
Last active June 21, 2024 11:39
Show Gist options
  • Select an option

  • Save primitiveshaun/42ed019fbe3b4bc26c8e0802a7eb58c7 to your computer and use it in GitHub Desktop.

Select an option

Save primitiveshaun/42ed019fbe3b4bc26c8e0802a7eb58c7 to your computer and use it in GitHub Desktop.
<?php
/*
Establish a connection using mysqli and checked for connection errors.
Use htmlspecialchars to escape output to prevent XSS attacks.
Updated SQL query to prevent SQL injection, though parameterised queries via prepared statements would be better.
Error Handling: Added error handling for the database connection and query execution.
Added error handling for file operations.
Code Structure: Improved readability by formatting the XML string more consistently.
Organized the code to separate database logic, XML generation, and file handling.
Make sure to replace placeholders like your_host, your_database, your_username, your_password, and "/path/to/your/feeds/index.xml" with actual values suitable for your environment.
*/
// Begin Function
function createRSSItem($post_title, $post_description, $post_link) {
$returnITEM = "<item>\n";
$returnITEM .= "<title>" . htmlspecialchars($post_title) . "</title>\n";
$returnITEM .= "<description>" . htmlspecialchars($post_description) . "</description>\n";
$returnITEM .= "<link>" . htmlspecialchars($post_link) . "</link>\n";
$returnITEM .= "</item>\n";
return $returnITEM;
}
// Database connection parameters
$host = 'your_host';
$db = 'your_database';
$user = 'your_username';
$pass = 'your_password';
// Create connection
$mysqli = new mysqli($host, $user, $pass, $db);
// Check connection
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
// Define the type of doc we're creating
$filename = "/path/to/your/feeds/index.xml";
$rootURL = "http://www.YOURSITE.com/feeds/";
$latestBuild = date("r");
$createXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$createXML .= "<rss version=\"0.92\">\n";
$createXML .= "<channel>\n";
$createXML .= "<title>TITLE_OF_YOUR_RSS_FEED</title>\n";
$createXML .= "<link>$rootURL</link>\n";
$createXML .= "<description>DESCRIPTION_OF_YOUR_FEED</description>\n";
$createXML .= "<lastBuildDate>$latestBuild</lastBuildDate>\n";
$createXML .= "<docs>http://backend.userland.com/rss092</docs>\n";
$createXML .= "<language>en</language>\n";
// Get the News Articles
$content_search = "SELECT title, description, seoTitle, timeStamp FROM articles ORDER BY timeStamp DESC";
if ($content_results = $mysqli->query($content_search)) {
while ($articleInfo = $content_results->fetch_object()) {
$page = $rootURL . htmlspecialchars($articleInfo->seoTitle) . ".html";
$description = htmlspecialchars($articleInfo->description);
$title = htmlspecialchars($articleInfo->title);
$createXML .= createRSSItem($title, $description, $page);
}
$content_results->free();
} else {
die('Error executing query: ' . $mysqli->error);
}
$createXML .= "</channel>\n</rss>";
// Finish it up
if ($filehandle = fopen($filename, "w")) {
fwrite($filehandle, $createXML);
fclose($filehandle);
} else {
die("Can't open the file");
}
$mysqli->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment