Jump to content

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';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.