I'm a new learner of PHP. I have written the following two files which do seem to work.
- I'd like to know if I can achieve the effect with just one script.
- I'd like to know if there's a better way to do this.
- 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 开发者_如何学C0x378 '.$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?)
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.
精彩评论