duansai1314 2011-07-05 16:34
浏览 27
已采纳

学习PHP - 有更好的方法吗?

I'm a new learner of PHP. I have written the following two files which do seem to work.

  1. I'd like to know if I can achieve the effect with just one script.
  2. I'd like to know if there's a better way to do this.
  3. The dox.php has to redirect to index.php every time, can I do away with the redirection?

To summarize the content of both files in words: Index.php creates a page with 8 images on it. All these 8 images have alternate versions which indicate that the image/button is active or not. Click on the button and the action is performed, click on it again and the action performed is reversed. (XOR function is used) - The current condition of the image/button is dictated by an external variable. (kind of like a feedback system)

Contents of: index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


<html><head>
<link rel="stylesheet" type="text/css" href="style2.css" />
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>Project ARC: Adeel's Room Controller</title>
</head><body>
<h1>Welcome to Project ARC</h1>
<?php $curx_val=shell_exec('sudo pin 0x378');
$curx_val=decbin($curx_val);
$curx_val=str_pad($curx_val,8,0,STR_PAD_LEFT);
//echo "$curx_val";
$bit_array=str_split($curx_val,1);
//echo "$bit_array[7]";
echo '<a href="dox.php?setvalue=128"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[0] . '.png"></a> ';
echo '<a href="dox.php?setvalue=64"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[1] . '.png"></a> ';
echo '<a href="dox.php?setvalue=32"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[2] . '.png"></a> ';
echo '<a href="dox.php?setvalue=16"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[3] . '.png"></a> ';
echo '<a href="dox.php?setvalue=8"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[4] . '.png"></a> ';
echo '<a href="dox.php?setvalue=4"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[5] . '.png"></a> ';
echo '<a href="dox.php?setvalue=2"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[6] . '.png"></a> ';
echo '<a href="dox.php?setvalue=1"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit' . $bit_array[7] . '.png"></a> ';
?><br>

<br>
</body></html>

Contents of dox.php

<html>
<title>PrimaryCommander</title>
<body>
<?php

//Variable Declaration
$set_val=$_REQUEST["setvalue"]; //Which light to switch on
$cur_val=shell_exec('sudo pin 0x378'); //Current lights which are ON

settype($set_val,"integer");

$xor_result=$set_val^$cur_val;
shell_exec('sudo parashell 0x378 '.$xor_result);
header( 'Location: index.php' ) ;

?>
</body>
</html>

Edit1: This script interfaces with the parallel port - thus the shell_exec command. The command 'sudo parashell 0x378 0' sets all pins of parallel port to 0.

Edit2: I understand that once the page has been rendered, it cannot be changed. But can I make the page so as to reflect live changes? (Somewhat like the facebook live feed feature?)

  • 写回答

1条回答 默认 最新

  • donglu9896 2011-07-05 16:43
    关注

    I won't retype all of your code, but something like this would do the trick:

    <?php
    
    // retrieve the current bit settings.
    $curx_val = shell_exec('sudo pin 0x378');
    $curx_val = decbin($curx_val);
    $curx_val = str_pad($curx_val,8,0,STR_PAD_LEFT);
    $bit_array = str_split($curx_val,1);
    
    
    if (isset($_GET['setvalue'])) {
       ... change the bit settings here
    }
    
    ?>
    ... display the page and current bit settings here
    <a href="index.php?setvalue=1"><img style="border: 0px solid ; width: 20px; height: 100px;" alt="bit0" src="images/bit <?php echo $bit_array[7]  ?>.png"></a>
    

    Using this, the first time you visit the page, setvalue won't be present in the script's query line, so the whole "change the bits" portion is avoided. The current bit settings are retrieved and displayed. Note that the link points at index.php instead now.

    Note that if this page is for general public use, then you'll have to be extraordinarily careful with how you do your shell_exec call, especially since you're using sudo. You're executing the 'pin' program with root privileges, so any malicious data submitted by the user can totally destroy your server. Consider the case where someone crafts a query that gets through your XOR calculation and produce ; rm -rf / &. Your script will now happily delete everything on the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题