簡體   English   中英

如何使用兩個if條件php和java腳本

[英]how to use two if condition php and java script

我正在使用此代碼,並且使JavaScript與URL一起使用,一切都很好,因此

<?php

// Settings to generate the URI
$secret = "P4ss#w0rD";                  // Same as AuthTokenSecret
$protectedPath = "/media/videos/mp4/";  // Same as AuthTokenPrefix
$ipLimitation = true;                   // Same as AuthTokenLimitByIp
$hexTime = dechex(time());              // Time in Hexadecimal
//$hexTime = dechex(time()+120);        // Link available after 2 minutes

$filee = basename($_GET['f']);          // The file to access
$fileName = "/$filee";                  // The file to access

// Let's generate the token depending if we set AuthTokenLimitByIp

if ($ipLimitation) {
  $token = md5($secret . $fileName . $hexTime . $_SERVER['REMOTE_ADDR']);
} else {
  $token = md5($secret . $fileName . $hexTime);
}

// We build the url

$url = $protectedPath . $token. "/" . $hexTime . $fileName;

?>
<script type="text/javascript" src="/kt_player/kt_player.js"></script>

<div id="kt_player" style="visibility: hidden">
    <a href="http://adobe.com/go/getflashplayer">This page requires Adobe Flash Player</a>
</div>

<script type="text/javascript">
var flashvars = {
    hide_controlbar: '1',
    hide_style: 'fade',
    preview_url: 'http://www.kernel-video-sharing.com/kt_player/poster.jpg',
    bt: '5',
    video_url: 'http://7.7.7.7<?php echo $url; ?>',
    video_url_text: '720p'

};

var params = {allowfullscreen: 'true', allowscriptaccess: 'always'};
kt_player('kt_player', '/kt_player/kt_player.swf', '854', '480', flashvars, params);
</script>

我想添加以下代碼:

<?php 

$user_ip = $_SERVER['REMOTE_ADDR'];
$res = file_get_contents("http://ipinfo.io/${user_ip}/country");
$country = trim($res);
if (in_array($country, array("US", "UK"))) {
    echo "hello us and uk";
} else {
    echo "";
} 

如果嘗試使用美國或英國的用戶,要獲得JavaScript的工作,我曾嘗試過,但我對PHP感到很失望。

如果我對您的理解正確,那么您只想向其IP檢查顯示來自英國或美國的用戶顯示一些內容。 由於使用PHP生成輸出,因此可以將所有可能對某些用戶隱藏的代碼放入if語句中,並在else顯示他們無法訪問此內容:

<?php

// Settings to generate the URI
$secret = "P4ss#w0rD";                  // Same as AuthTokenSecret
$protectedPath = "/media/videos/mp4/";  // Same as AuthTokenPrefix
$ipLimitation = true;                   // Same as AuthTokenLimitByIp
$hexTime = dechex(time());              // Time in Hexadecimal
//$hexTime = dechex(time()+120);        // Link available after 2 minutes

$filee = basename($_GET['f']);          // The file to access
$fileName = "/$filee";                  // The file to access

// Let's generate the token depending if we set AuthTokenLimitByIp

if ($ipLimitation) {
  $token = md5($secret . $fileName . $hexTime . $_SERVER['REMOTE_ADDR']);
} else {
  $token = md5($secret . $fileName . $hexTime);
}

// We build the url
$url = $protectedPath . $token. "/" . $hexTime . $fileName;

// check the country
$user_ip = $_SERVER['REMOTE_ADDR'];
$res = file_get_contents("http://ipinfo.io/${user_ip}/country");
$country = trim($res);

// show content only to the users in the UK or USA
if(in_array($country, array("US", "UK"))) { ?>
    // your javascript code - make sure to check if this is right
    <script type="text/javascript">

    var flashvars = {

        hide_controlbar: '1',

        hide_style: 'fade',

        preview_url: 'http://www.kernel-video-sharing.com/kt_player/poster.jpg',

        bt: '5',

        video_url: 'http://7.7.7.7<?php echo $url; ?>',
        video_url_text: '720p'

    };

    var params = {allowfullscreen: 'true', allowscriptaccess: 'always'};

    kt_player('kt_player', '/kt_player/kt_player.swf', '854', '480', flashvars, params);

</script><?php
} else {
    echo "You are not allowed to view this content!";
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM