22 January 2019

Self Hosting

Self Hosting WHX.IO

Don't forget to checkout our API for more actions like this.


If you wish to redirect users on your own domain/server like www.my-website-url.com/go/whx-shortened-id ? you could use this simple code (using /api/v1/url/:id to validate the shorturl) Heres an example with PHP: Try this navigator: https://whx.io/test_navigator.php?id=whx-selfhosting (navigates to this page via whx.io)

<?php

    if(isset($_GET['id']))
    {

        /* 
            validation 
            Use this no only allow access to shorturls that were created/called via your domain/ip-address!
            It's recommended that you have a unique ip address for this to work! this allows us to not have 
            people take advantage of your domain and use their own shorturls with your domain.
        */
        $id = $_GET['id']; /* whx shorturl id */
        $obj = file_get_contents('https://whx.io/api/v1/ip_validate/'.$id);
        $obj_decode = json_decode($obj, true);

        if($obj_decode['server_info'][0]['server_status'] == 'success') 
        {
            $obj = file_get_contents('https://whx.io/api/v1/url/'.$id);
            $obj_decode = json_decode($obj, true);

            /* for printing the whole output */
            // var_dump($obj_decode);

            /* check if the shorturl exists */
            if($obj_decode['server_info'][0]['server_status'] == 'success') 
            {
                /* navigates through whx */
                header('Location: https://whx.io/'.$obj_decode['link_data'][0]['link_tracking_id']); 
            } else {
                /* 
                    print error message 
                    you may try to prompt this message by replacing the id with an invalid one.
                */
                echo 'Error: '.$obj_decode['server_info'][0]['server_message'];
            }
        } else {
            echo 'Error: '.$obj_decode['server_info'][0]['server_message'];
        }
    } else {
        echo 'Error: id parameter not set!';
    }

?>



Track your links

To track visitors for that url use this script Try this navigator: https://whx.io/test_navigator_visitor_check.php?id=whx-selfhosting (navigates to whx.io dashboard). Note that you don't have to navigate to whx.io dashboard, you could use our api and build your own dashboard.

<?php

    if(isset($_GET['id']))
    {

        /* let's first grab the mylink_id */
        $id = $_GET['id']; /* whx shorturl id */
        $obj = file_get_contents('https://whx.io/api/v1/url/'.$id);
        $obj_decode = json_decode($obj, true);

        if($obj_decode['server_info'][0]['server_status'] == 'success') 
        {

            $mylink_id = $obj_decode['link_data'][0]['link_mylink_id'];
            /* for printing each visitor result */
            header('Location: https://whx.io/edit/'.$mylink_id); 
        } else {
            echo 'Error: mylink_id could not be fetched!';
        }
    } else {
        echo 'Error: id parameter not set!';
    }

?>



URL Collection

To get the whole URL collection created under your website ip address use this.

<?php

    $obj = file_get_contents('https://whx.io/api/v1/collection/');
    $obj_decode = json_decode($obj, true);

    if($obj_decode['server_info'][0]['server_status'] == 'success') 
    {

        foreach($obj_decode['link_data'] as $shorturl)
        {
            echo 'www.mydomain.com/sub/'.$shorturl['link_tracking_id'].'
'; /* for printing each shorturl */ } } else { echo 'Error: mylink_id could not be fetched!'; } ?>


And if you wish to get rid of ".php" from the end create a file named .htaccess or add this to your existing:

    
    RewriteEngine On
    # your directory here
    RewriteBase /

    #change "test_navigator.php" to whatever your script is called
    RewriteRule ^go?$ test_navigator.php.php?id=$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) test_navigator.php?id=$1 [L]