gruebz Posted July 15, 2010 Share Posted July 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207825-if-statement-dependent-on-cookie-values/ Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/207825-if-statement-dependent-on-cookie-values/#findComment-1086408 Share on other sites More sharing options...
gruebz Posted July 15, 2010 Author Share Posted July 15, 2010 hey... my developer says this looks like it makes sense. thanks heaps for your help Quote Link to comment https://forums.phpfreaks.com/topic/207825-if-statement-dependent-on-cookie-values/#findComment-1086473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.