Customer Sign In

upLynk

Embedding the upLynk player Part 5: Geo-filtered access to content

Tutorial thumbnail

Summary Combine the concepts from the previous Embedding Tutorials and add in user authentication to demonstrate geo-filtered conditional access to content.

Skill level required Basic understanding of PHP, and html

Time to complete 20-30 minutes

Things you’ll need

Introduction

This tutorial demonstrates a simplified, yet complete demonstration of blocking access to content using a geo-filter. This example should not be considered production ready. Security, scale, etc. are outside the scope considered in this tutorial.

Step 1. Setting up the Geo database

The database used in this example is freely available from http://www.maxmind.com/app/geolite. Instructions for installing the database will vary per platform. Many Linux server flavors provide packages that will install the geo database automatically.

Step 2. Setting up the GeoIP API for PHP

There are several methods available to get the PHP GeoIP APIs on your server. As with the geo database, many Linux server flavors provide packages that will install the GeoIP APIs automatically.

Step 3. The application

When the user makes their web page request, their IP address is used to query the geo database for their country code. That code is then matched against $REQUIRED_COUNTRY_CODE. If there is a match, the user is presented the video. If there is not a match, the user is presented the apology page, and no video is available.

The default for this code is to allow IP addresses with country code of "US", as well as reserved or private IP addresses, to view the video. You can change $REQUIRED_COUNTRY_CODE to test denied access. You can also pass False in the call to ViewerCanWatch(), to deny private and reserved IP addresses access.

The application is composed of 2 php source code files:

  1. geo.php - Provides the application logic as well as the html templates for our 2 pages, the access denied page and the video page.
  2. uplynk.php - PHP utilities provided by upLynk.

In-line comments in the source code files provide more information about the application flow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*
*
* geo.php
* Embedding player 5 Application Script and HTML
*
*/

// Include the upLynk Utility Script
include("uplynk.php");

// The user's IP Address
$ip = $_SERVER["REMOTE_ADDR"];

// Set this to the country code you would like to limit viewer origination to
$REQUIRED_COUNTRY_CODE = "US"; // ISO 3166-1 alpha-2
//$REQUIRED_COUNTRY_CODE = "CA"; // ISO 3166-1 alpha-2

function ViewerCanWatch($allow_private=True){
    // bring the global vars into scope
    global $ip, $REQUIRED_COUNTRY_CODE;
    $is_private_or_reserved = !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);

    if($is_private_or_reserved){
        if($allow_private){
            return True;
        }
        return False;
    }

    if($REQUIRED_COUNTRY_CODE == geoip_country_code_by_name($ip)){
        return True;
    }
    return False;
}


// Setup the var for use in the template
$can_view = ViewerCanWatch();  // Pass false to disallow private or reserved IP addresses

?>

<! DOCTYPE html>
<html>
    <head>
        <title>upLynk Geo App</title>
        <?php if($can_view) : ?>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript" src="http://storage.uplynk.com/js/swfobject.js"></script>
        <script type="text/javascript" src="http://storage.uplynk.com/js/uplynk.js"></script>
        <?php endif; ?>
    </head>
    <body>
        <?php if(!$can_view) : ?>
        <div id="not_logged_in">
            <h1>upLynk Geo App</h1>
            <h2>Welcome!</h2>
            <p>We're sorry, but this video is not available for viewing in your area.</p>
        </div>
        <?php else : ?>
        <div id="navigation"><a href=".">Logout</a></div>
        <div id="logged_in">
            <h1>upLynk Test App</h1>
            <h2>Welcome!</h2>
            <div id="videoPlayer"></div>
        </div>
        <script type="text/javascript">
            $(function(){
                $('#videoPlayer').player('play','<?php echo GenerateToken('http://content.uplynk.com/d7d8471a20c444a59d66c6f9887593e7.m3u8');?>');
            });
        <?php endif; ?>
        </script>
    </body>
</html>
		
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/*
*
* uplynk utilties for use with PHP
*
*/


// Token generation helper function
function GenerateToken($uri)
{
    $SECRET = 'T/YKeTCO0oxbtARDrlm1cBj5jIDXAz1vm0yilBQd'; // See DRM Docs
    $msg = array();
    $msg['exp'] = time() + 300; // 5 minutes from now
    $msg['ct'] = 'a'; // asset
    $parts = parse_url($uri);
    list($part1, $part2) = explode('.',$parts['path']);
    $msg["cid"] = substr($part1,1);
    $msg['iph'] = hash('sha256', $_SERVER['REMOTE_ADDR']);
    $msg['sig'] = hash_hmac('sha256', http_build_query($msg), $SECRET);

    return $uri . '?' . http_build_query($msg);
}

?>
		

You can copy the code above and deploy it on your own server for testing. Save the files with their respective names.

Finished!

You've created a simple player requiring the user to pass the geo-filter to allow video playback.

Back to tutorials