Developer

Promote your app on social more effectively with this PHP redirect

A simpler way to get your app users to the right app store when promoting it on social media is to use this PHP redirect script.

melodiousgoogleplayhero112614.jpg
Image: Screenshot by Mary Weilage/TechRepublic

Services that shorten URLs such as Bitly and TinyURL are particularly helpful to app developers, because a link to your app in the marketplace tends to look something like this: https://play.google.com/store/apps/details?id=com.cluckeyetea.melodious&hl=en.

When I first released my latest game, Melodious, on Google Play, I found shortened URLs worked great not only for sharing in game with other players, but also on Facebook and Twitter. However, as the popularity of Melodious grew, and I made it available for iOS and Kindle Fire, even with shortened URLs, the user experience was less than ideal. My initial share sheet text was simply:

Have you tried Melodious? Download it today! http://goo.gl/ZDvZrD

Once the game was available in multiple markets, I found myself having to tweet things like:

Have you tried Melodious? Download it today!

On Google Play: http://goo.gl/ZDvZrD

On Apple iTunes: http://goo.gl/PmiGDw

On Amazon Kindle: http://goo.gl/wRRX9i

Not only is the above example a lot to expect the user to read, but it also violates some of the length restrictions of some social media outlets. Plus, it's horrible for sharing via SMS, sometimes breaking up into multiple messages that split on a URL, making the link completely worthless.

Fortunately, I know a bit of PHP, and when backed into a corner I'm not afraid to use it! My solution was to create a simple redirect script that would send a user to a particular store based on the web client accessing my page. So if the user hit the link from a Safari client, I'd send them to iTunes, a Silk client would take you to Amazon's App Store, Chrome would go to Google Play, and as a fallback anything else would display my lengthy but complete list of all platforms and links where a user could get the app.

This approach has worked really well for me. So while I usually share Java code in my TechRepublic columns, in this instance, I thought I'd share my PHP script. Enjoy!

 <?php
 
$SAFARI_URL = " http://goo.gl/PmiGDw ";
$CHROME_URL = "http://goo.gl/ZDvZrD";
$OTHER_URL = "http://www.mydomain.com/default.html";
$AMAZON_URL =  “http://goo.gl/wRRX9i";
 
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
 
function str_present($str,$substr)
{
	$pos = strpos($str,$substr);
 
	if($pos === false) {
 		return false;
	} else {
 		return true;
	}
}
 
if (str_present($HTTP_USER_AGENT, "Silk")) {
   Header ("Location: " . $AMAZON_URL);
} else if (str_present($HTTP_USER_AGENT, "Chrome")) {
    Header ("Location: " . $CHROME_URL);
} else if (str_present($HTTP_USER_AGENT, "Safari")) {
    Header ("Location: " . $SAFARI_URL);
} else {
    Header ("Location: " . $OTHER_URL);
}
?>

Figure A

melodiusandroidfiga112614.png
Image: Screenshot by William J. Francis/TechRepublic

My URL when accessed from an Android phone or tablet.

Figure B

melodiusiosfigb112614.png
Image: Screenshot by William J. Francis/TechRepublic

My URL when accessed from an iPhone or iPad.

About William J. Francis

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

Editor's Picks

Free Newsletters, In your Inbox