0

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');
?>
asked Jul 17, 2013 at 18:16
3
  • It might not be the whole issue, but your form's input is hport; your code is looking for hport1. Commented Jul 17, 2013 at 18:19
  • and what is the value of $_POST['hport'] when you submit the form? Commented Jul 17, 2013 at 18:19
  • I haven't done PHP in a while, but isn't there an isset function you can use? Commented Jul 17, 2013 at 18:20

3 Answers 3

1

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.

answered Jul 17, 2013 at 18:55
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Yes that was the issue. It was just a typo and worked after I fixed it up. We didn't need the isset function
1

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
}
answered Jul 17, 2013 at 18:23

Comments

0

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');
}
answered Jul 17, 2013 at 20:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.