How to return a value based on Query String?
-
10-12-2019 - |
Question
I have this code sample (written by someone else) that parses the website, and finds the latest version's URL of each type of file (beta/developmental/release) and returns them.
I would like to be able to do something like bukkit.php?version=beta and have it automatically go to that URL and start the download. How would I go about doing something of that nature?
<?php
require_once('simple_html_dom.php');
$html = file_get_html('http://dl.bukkit.org/downloads/craftbukkit/');
$dom = new DOMDocument;
libxml_use_internal_errors(true);
echo $dom->loadHTML($html) ? "success<br/>" : "failed<br/>";
libxml_clear_errors();
$dom->preserveWhiteSpace = true;
foreach ($dom->getElementsByTagName('div') as $element){
if($element->getAttribute('class') == "innerContent"){
foreach ($element->getElementsByTagName('a') as $link) {
if( $link->getAttribute('class') == "tooltipd")
{
echo $link->getAttribute('href')."<br/>";
}
}
}
}
?>
Solution
Rather use a regular expression to find the url
$html = file_get_html('http://dl.bukkit.org/downloads/craftbukkit/');
preg_match("/bukkit\.php\?version=([beta|alpha|rc]+)/i", $html, $matches);
header("Location: ". $match[1]);
OTHER TIPS
Something like this might be what you're looking for:
$version = $_GET['version'];
if($version == 1) {
header("Location: http://www.domain.com/version1");
} else if($version == 2) {
header("Location: http://www.domain.com/version2");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow