function hashDecode($hashedUrl) { $urlEncode = new UrlEncode(); $result = array(); //No hash was given as input if (!$hashedUrl) { $result["error"] = "No input given"; } else { $hashedUrl = $urlEncode->parseHash($hashedUrl); if ($hashedUrl != -1) { $id = $urlEncode->decodeToOriginalUrl($hashedUrl); //Database stuff global $dbConnStr; //This comes from dbConfig.php $dbConnection = pg_connect($dbConnStr); $query = sprintf("SELECT url FROM urls WHERE id = %d", $id); //sprintf to reduce SQL injection attack $dbResult = pg_query($query); $row = pg_fetch_assoc($dbResult); if ($row) { $result["originalUrl"] = $row["url"]; } else { //The URL does not exist in our database $result["error"] = "Url does not exist: {$hashedUrl}"; } } else { $result["error"] = "Hashed url host incorrect"; } } return json_encode($result); }