0

I'm trying to send a variable in shell command and get output on screen. When I white ping ping -c 5 google.com it's working, but when a put variable $username it's not. Why my variable is not working?

<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="7.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>

<?php
$username = $_POST["username"];
$result = shell_exec('ping -c 5 $username');
echo "<pre>$result</pre>";
?>
2
  • 2
    This is an enormous security risk. What if I post ; rm -rf / to your script? Commented Sep 30, 2014 at 12:13
  • Thanks for information! I'll try to do something with it Commented Sep 30, 2014 at 12:23

1 Answer 1

3

You are using single quotes, you need to concatenate the $username or use double quotes

<?php
$username = $_POST["username"];
$result = shell_exec('ping -c 5 ' . $username);
echo "<pre>$result</pre>";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

It's true. Wrong quotes. Thanks kaseOga.

Your Answer

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.