PHP 变 Javascript

PHP var to Javascript

我有以下代码可以工作,但速度很慢,有什么改进周转时间的建议吗?

我怎样才能让它只接受从 1 到 10 的数字并拒绝 alpha

"h**p://mydomai.any/handler.php?msg1=10" 好的

"h**p://mydomai.any/handler.php?msg1=14" 不行

"h**p://mydomai.any/handler.php?msg1=HELLO" 不行(现在它接受所有)

handler.php

<?php
header("HTTP/1.1 200 OK");
if (isset($_REQUEST['msg1'])) {
    $msg1 = $_REQUEST['msg1'];
    ?>
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://signage.me/demo/sendCommand.js"></script>
    <script type="text/javascript">
      $(document).ready(function()
      {
           sendCommand("galaxy.signage.me", "username", "password", "13", "new1", (msg1 = <?php echo (json_encode($msg1)); ?>));  
         });
    </script>
    <?php
}
?>

is_numeric — 判断变量是数字还是数字字符串。

<?php
header("HTTP/1.1 200 OK");
if (isset($_REQUEST['msg1']) && is_numeric($_REQUEST['msg1']) && $_REQUEST['msg1'] >=1 && $_REQUEST['msg1'] <=10 ) {
    $msg1 = $_REQUEST['msg1'];
    ?>
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://signage.me/demo/sendCommand.js"></script>
    <script type="text/javascript">
      $(document).ready(function()
      {
           sendCommand("galaxy.signage.me", "username", "password", "13", "new1", (msg1 = <?php echo (json_encode($msg1)); ?>));  
         });
    </script>
    <?php
}
?>

使用 http://php.net/manual/en/function.intval.php$msg1 转换为整数并在将其注入 javascript 代码之前检查其值。