0

Bit of a organized mess. I'm protecting my mp3/ogg files from direct access by serving them through a PHP file. I was having some big issues with serving audio through PHP on mobile devices so I installed Mobile_Detect so I could conditionally serve the direct audio url to mobile users and the PHP url to desktop users. I created shortcodes in functions.php

include_once( get_stylesheet_directory() . '/Mobile_Detect.php');


//ISMOBILE SHORTCODE
add_shortcode( 'ismobile', 'ismobile_shortcode' );
function ismobile_shortcode( $atts, $content = null ) {
$detect = new Mobile_Detect();
if( $detect->isMobile() ) {
return do_shortcode($content);
} else {
return '';}
}

//ISNOTMOBILE SHORTCODE
add_shortcode( 'isnotmobile', 'isnotmobile_shortcode' );
function isnotmobile_shortcode( $atts, $content = null ) {
$detect = new Mobile_Detect();
if( !$detect->isMobile() ) {
return do_shortcode($content);
} else {
return '';}
}

Now when I have an audio player in a post I make 2 instances of it wrapped in each shortcode. Mobile users see the audio player that links to the direct audio url, desktop users see the one linked to the PHP script.

Everything works great except for Safari desktop browser. The audio player won't seek and the end time shows NaN:NaN. From Googling the issue, it seems this has been a problem since 2010 with Safari and serving audio through PHP.

I figured if I could detect the Safari desktop browser I can include it in my "ismobile" shortcode so the real audio link gets sent to Safari desktop users. Make sense?

I found this https://stackoverflow.com/a/9851769/1131465 but I don't know how to implement it for my specific case.

EDIT: With Vinod's answer these are my new shortcode functions that work to detect Safari desktop:

include_once( get_stylesheet_directory() . '/Mobile_Detect.php');
//ISMOBILE SHORTCODE
add_shortcode( 'ismobile', 'ismobile_shortcode' );
function ismobile_shortcode( $atts, $content = null ) {
$detect = new Mobile_Detect();
global $is_safari;
if ( $detect->isMobile() ) {
return do_shortcode($content);
} elseif ( $is_safari ) {
return do_shortcode($content);
} else {
return '';}
}

//ISNOTMOBILE SHORTCODE
add_shortcode( 'isnotmobile', 'isnotmobile_shortcode' );
function isnotmobile_shortcode( $atts, $content = null ) {
$detect = new Mobile_Detect();
global $is_safari;
if( !$detect->isMobile() && !$is_safari ) {
return do_shortcode($content);
} else {
return '';}
}
  • you can not detect browsers on server side, the best you can do is detect user agent which is easy to spoof. You basically wasting time trying implement this kind of DRM – Mark Kaplun Apr 21 '17 at 10:08
  • @MarkKaplun It's not wasting time if it works, which it currently does. Desktop users can't view source or open Network tab and grab the link and download a file, it comes back with an error. Same with download websites. I understand it's not 100% foolproof but I'm ok with that. – Nate M. Apr 21 '17 at 11:03
  • this can not work, just like 1+1 is never 3. Your DRM is broken, but if you like to be in an illusion it works, that is fine with me. – Mark Kaplun Apr 21 '17 at 12:42
  • My goal was to stop desktop visitors from viewing the source of the page or Inspect>Network and grabbing a direct mp3 link and downloading it. My goal is accomplished because it does work. I think you're forgetting about the php file that serves the audio, it doesn't allow direct access to mp3/ogg files. – Nate M. Apr 21 '17 at 13:52
2

You can use WordPress global variable $is_safari like following to detect whether the browser is safari browser.

global $is_safari;

if ( $is_safari ) {
    // This is safari browser
}

To detect whether the browser is safari desktop browser, you can use condition like following.

global $is_safari;

if ( ! wp_is_mobile() && $is_safari ) {
    // This is desktop safari browser
}
  • Didn't know WordPress had that built it, awesome. So far it works great on BrowserStack, thanks! – Nate M. Apr 21 '17 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.