Okay so I have a file called proxy.php with these contents and what I want to do with it is that, if any of the forms are filled with a value and submitted, the "if" check should become true and run the commands but I have a problem and even though I submit a value, it doesn't go into the "if" check. If I put the commands out of "if" check, they start to work but not inside them.
<html>
<body>
<br>
<form action="proxy.php" method="post">
<br>
Host Port 1: <input type="text" name="hport" />
Server Port 1: <input type="text" name="sport"/>
<br>
Host Port 2: <input type="text" name="hport2"/>
Server Port 2: <input type="text" name="sport2"/>
<br>
<input type="submit" />
</form>
</body>
</html>
<?php
include('Net/SSH2.php');
$_POST["ip"]="iphere";
$_POST["pass"]="passhere";
$ssh = new Net_SSH2($_POST["ip"]);
if (!$ssh->login('root', $_POST["pass"])) {
exit('Login Failed');
}
if($_POST['hport1']){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}
if($_POST['hport2']){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy2 -X quit');
echo $ssh->exec('Run these commands');
}
echo $ssh->exec('exit');
?>
3 Answers 3
The value $_POST['hport1'] is null because you are posting 'hport' from html. Try with that change.
if($_POST['hport']){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}
If the problem still persists, use isset($_POST['hport']) to check whether the value for the variable 'hport' is set or not. You can check the POST values mannually, use
<?php var_dump($_POST); ?>
OR
<?php
echo '<pre>' . print_r($_POST) . '</pre>';
?>
for displaying the $_POST array values in a readable format. Hope this will help you.
1 Comment
could try and use isset to check the details.
if(isset($_POST['Name of field to check for'])){
////CODE HERE
}
An alternative might be to check if the form was submitted and then do something
if(empty($_POST) === false){
///CODE HERE
}
Comments
Use isset() & empty() inbuilt function of PHP to check the variables..
if(isset($_POST['hport'] && !empty($_POST['hport'])){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy1 -X quit');
echo $ssh->exec('Run these commands');
}
if(isset($_POST['hport2'] && !empty($_POST['hport2'])){
echo $ssh->exec('ls');
echo $ssh->exec('screen -S Proxy2 -X quit');
echo $ssh->exec('Run these commands');
}
hport; your code is looking forhport1.