I am building a visitor tracking system and I want it to be as much as abstract as possible, without having to put static variables inside the code. The campaign table is structure this way:
ID - NAME - URL - REDIRECT URL
Where ID is the ID
of the campaign ( auto-incremented ), NAME
is the name of the campaign, URL
is the url of the campaign.
The fact is that I should get the ID of the campaign based on the campaign we actually are and I am doing it by comparing the current URL with the URL in the DB. By doing this i get the campaign ID and do the rest of the work.
The problem is that with my function I correctly get the URL of the campaign but when comparing it with the URLs in the DB not always it returns what expected.
I tried to get the URL with all the possible variates but sometimes the URL does not return in the right way and the ID is not recognized in the DB.
How may I solve this issue? Is there another logic I may use instead of comparing the URL?
$campaign = getCampaignDetails("url", cleanUrl($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
function cleanUrl($url) {
$url = str_ireplace("http://www.", "", $url);
$url = str_ireplace("http://", "", $url);
$url = str_ireplace("www.", "", $url);
$url = str_ireplace(".php", "", $url);
$url = str_ireplace("https://www.", "", $url);
$url = str_ireplace("https://", "", $url);
return $url;
}
function getCampaignDetails($table, $var) {
$query = "SELECT * FROM _campains WHERE ".$table." = '$var'";
$result = mysql_query($query) or die("Getting campaign details via url failed: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
return $row;
}
}
Example of values of a campaign in the DB:
ID = 1, Name = tgcampaign, URL = site.com/campains/tgadv
For now to avoid unexpected errors I am just passing the ID directly in the file, but this is something I would like to avoid, like below:
$campaignId = "1";
$campaign = getCampaignDetails("id", $campaignId);