Jump to content

If Statement Dependent on Cookie Values


gruebz

Recommended Posts

Basically I am just trying to find out if this is possible or not. I have no real PHP knowledge but I am asking on behalf of a developer.

 

I am wanting to display a different phone number based on where the visitors has come from (CPC, Organic e.t.c). The Visitors traffic source is stored in the UTM_Source Cookie. Is it possible to create an If Statement which runs when the page loads which can show a different number depending on the value of the UTM_Source Cookie. Basically something like this:

If the cookies utm_medium = cpc then use x phone number

If the cookies utm_medium = organic then use y phone number

Else use z phone number

 

If it matters the phone number will be stored in a Wordpress theme.

 

Any help much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/207825-if-statement-dependent-on-cookie-values/
Share on other sites

There's a number of ways of doing this. If a database is a little overkill, you may be best going with an array:

 

$source_phone_numbers = array(
    'CPC' => 'xxx',
    'Organic' => 'xxx',
);

if (isset($_COOKIE['UTM_Source_Cookie']))
{
    $source = $_COOKIE['UTM_Source_Cookie'];

    if (array_key_exists($source, $source_phone_numbers))
    {
        $phone_number = $source_phone_numbers[$source];
    }
}

if (!isset($phone_number))
{
    $phone_number = 'default number';
}

echo $phone_number;

 

This method's a lot better than a series of if..else conditions (even a switch in my opinion) as it allows room for growth with minimal configuration.

 

Edit

 

Actually reading back your post, this may be over kill as well. Depends how many different sources you're going to have I guess. Best suggestion would be a switch:

 

$source = isset($_COOKIE['UTM_Source_Cookie']) ? $_COOKIE['UTM_Source_Cookie'] : '';

switch ($source)
{
    case 'CPC':
        $phone_number = 'xxx';
        break;
    case 'Organic':
        $phone_number = 'xxx';
        break;
    default:
        $phone_number = 'xxx';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.