如何使用两个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!";
}