1

I have below code that works but it is slow is there any suggestion to improve turnaround.

How can I have it accept only numbers from 1 to 10 and reject alpha

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

"h**p://mydomai.any/handler.php?msg1=14" not ok

"h**p://mydomai.any/handler.php?msg1=HELLO" no ok (now it accepts all)

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
}
?>
Chris
  • 15
  • 5
  • 1
    check http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript maybe helpful – Mostafa Jul 20 '16 at 04:35
  • @Mostafa selection technique to pass data (json) was from that post. The question now is why it takes so very long to execute – Chris Jul 20 '16 at 06:27

2 Answers2

0

is_numeric — Finds whether a variable is a number or a numeric string.

<?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
}
?>
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
0

Use http://php.net/manual/en/function.intval.php to convert $msg1 to an integer and check its value before injecting it in the javascript code.

Carles Andres
  • 1,761
  • 1
  • 15
  • 22