dongyong2906 2013-06-10 08:57
浏览 64

PHP通过比较当前URL获取数据库中的ID

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);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 聚类分析或者python进行数据分析
    • ¥15 逻辑谓词和消解原理的运用
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号