Search code examples
phpvideomedia-playermediajwplayer

How to make video play on web by using url, but can not access by direct link?


How to make video play on web by using url, but can not access by direct link ?

Now I use jw player

<script type="text/javascript">
                    jwplayer("mediaplayer1889082201").setup({
                            flashplayer: "jwplayer/jwplayer.flash.swf",
                            file: "http://testhost.com/myproject/files/1/video/coursecreeklruprr4nags7ee1codeschool_756.mp4",
                            image: "http://testhost.com/myproject/files/1/video/coursecreeklruprr4nags7ee1codeschool_756.jpg",
                            width: "800",
                            height: "510",
                            stretching: "uniform",
                            skin: "jwplayer/jwplayer-skins-free/six.xml",
                            abouttext: "myproject",
                            aboutlink: "myproject",
                    });
            </script>

So I can using http://testhost.com/myproject/files/1/ to access to folder (Look this link by inspect element)

I want video play on jw player.But i don't want user on web access by direct url to folder that contain video.

Thaks you.


Solution

  • Ok, I understand you want to serve the video file to the script, but you don't want users being able to navigate to the video url and download/view it from there.

    A quick example using PHP

    Change your code to:

    <script type="text/javascript">
                        jwplayer("mediaplayer1889082201").setup({
                                flashplayer: "jwplayer/jwplayer.flash.swf",
                                file: "video.php?file=coursecreeklruprr4nags7ee1codeschool_756.mp4",
                                image: "http://testhost.com/myproject/files/1/video/coursecreeklruprr4nags7ee1codeschool_756.jpg",
                                width: "800",
                                height: "510",
                                stretching: "uniform",
                                skin: "jwplayer/jwplayer-skins-free/six.xml",
                                abouttext: "myproject",
                                aboutlink: "myproject",
                        });
                </script>
    

    As you can see, we are sending a file request to video.php, then we'll create that file serving the video only when the referrer is your webpage which will be able to show it.

    Go ahead and create video.php in the same folder.

    First we need to check if a file has been given in the url, like this:

    <?php
    if(isset($_GET['file']) {
    }
    

    Then we check if the referrer is your page, like this:

    if (strpos($ref,'testhost.com/myproject/files/1') !== 0){
    }
    

    If it's your page we can start serving your file, like this:

    $path = 'video/' . $_GET['file'];
    header("X-Sendfile: $path");
    

    So the whole thing together:

    <?php
    if(isset($_GET['file']) {
        if (strpos($ref,'testhost.com/myproject/files/1') !== 0){
            $path = 'video/' . $_GET['file'];
            header("X-Sendfile: $path");
        }
    }
    

    Look out; this is a very quick EXAMPLE, not a safe-and-ready-to-use-script! Their has to be security checks build in, and you're serving a file even when it's not existing, so that kind of checks still has to be made to make it ready for use.

    Good luck with your project.