PHP simple whois script html form - php

Im just creating a simple tool in PHP what uses the built in whois command in linux and then echo's the response back to the user. Everything I have attempted has either failed or I have messed up somewhere.
<html>
<body>
<form method="POST" action="">
<input type="text" name="cmd1">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
$cmd = $_POST['cmd1'];
echo "<pre>".shell_exec('whois ', $cmd)."</pre>";
?>
For me nothing happens, for the record I am new to PHP and I would like just a little expertise. Im the only one that is going to be using the script. Thank you.

You need to check whether a POST variable was submitted, so you don't try to run whois when you're first displaying the form. You should also escape the parameter to prevent command injection.
And you need to display the results inside the <body> of the HTML.
Another problem: you need to concatenate 'whois ' with the parameter, not pass them as separate arguments to shell_exec (it only takes one argument, and ignores the extra argument, so you were just executing the command whois with no domain).
<html>
<body>
<form method="POST" action="">
<input type="text" name="cmd1">
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['cmd1'])) {
$cmd = $_POST['cmd1'];
echo "<pre>".shell_exec('whois ' . escapeshellarg($cmd))."</pre>";
}
?>
</body>
</html>

You can also use Net_Whois:
Install:
pear install Net_Whois
Usage:
<?php
require_once "Net/Whois.php";
$server = "whois.denic.de";
$query = isset($_POST['cmd1']) ? $_POST['cmd1'] : 'phpcrawler.de';
$whois = new Net_Whois;
$data = $whois->query($query, $server);
echo $data;
?>

Related

Get variable from Post Data (PHP)

I really don't know why my code isn't working! Let me clear it by example..
I got a file named 'index.html' example...
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="test">
<input type="submit">
</form>
</body>
</html>
And of course the form action file test.php .. example..
<?php
$something = ($_POST["test"]);
// from here I have some PHP codes and this "something" variable will be process in this codes and will print out something spacial..
?>
Now example If I post "Hello, It's not working" then the output will show a spacial design.
But Instead process, it's just printing out whatever I submit in that form.
But when I manually add the variable to "something" and if I execute the "test.php" . Example..
$something = "Hello, It's not working";
Then it works perfectly..
And yes. Also tried GET method.. It's still same as POST.
This is my first question here..
Thanks for any help and suggestions!
First Convert, index.html to php and follow this code:-
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" value="" name="test">
<input name="submit" value="submit" type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$something = $_POST["test"];
}
?>
That's correct _POST:
<?php
//NO ($_POST["test"])
$something = $_POST["test"];
?>

PHP execute shell script and provide inputs for prompts

I have an apache server on a raspberry pi. I have a basic program where the user inputs a string into an html form box. Clicking submit sends the entry to a php file, which then calls a shell script, and passes the entry as a prompt. If you look at the php below, it has a variable with the user input, and it calls the shell file main.sh. How can I provide the shell program that variable as an input. I have looked at proc_open, but haven't been able to get it to work. Thanks
Index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="script.php" method="post" enctype="multipart/form-data">
Address: <input type="text" name="Address" value=""><br>
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
script.php:
<?php
if(isset($_POST["submit"])) {
if($_POST['Address'] != "")
{
$entry = $_POST['Address'];
$output = shell_exec ("./main.sh");
echo $output;
}
}
?>
and finally the shell file: main.sh:
echo -n "Do you like pie (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
echo Yes
else
echo No
fi
you can pass variable to shell script as follows
$output = shell_exec("./main.sh $entry");
and in shell script , Add x = $1 . $x would be variable, with your user input value.
Look for this post for further details https://stackoverflow.com/a/19460686/3086531

Cannot echo the values from a simple php form

First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.

Executing BashScript with PHP Issues

I have a bash script that's tied into some python scripts that I would like to execute within a webpage and cat the file it creates on to the page. Currently I haven't been able to execute the script at all (tested the script multiple times to ensure it is in fact working), and I can't seem to find where my error is. The first set of code is from my 'dashboard.php' file which is in /var/www. I have changed the permissions for the scripts to 777 and made executable.
<html>
<body>
<form action="welcome.php" method="post">
IP/CIDR Input: <input type="text" name="cidriptext" />
Single IP: <input type="checkbox" name="singlech" value="single" />
CIDR Range: <input type="checkbox" name="cidrch" value="cidr" />
<input type="submit" value="Submit">
</form>
<form action="welcome.php" method="post">
List of IP's: <input type="text" name="listname" />
<input type="submit" value="Submit">
</form>
</body>
</html>
This second script is the page ('welcome.php') it reaches out to execute the script with the input from the form. At this point I don't expect the $listname_text to work properly, but I do expect to get the 'Incorrect Input' error when no boxes are checked, the 'testing' output at the end, and the files being created on the backend.
<?php
$ipcidr_text = $_POST['cidriptext'];
$listname_text = $_POST['listname'];
if !(empty($listname_text):
shell_exec('/var/www/getStarted -l $ipcidr_text');
$output0 = shell_exec('echo "List of IP's"');
echo "<pre>$output0</pre>";
elseif (isset($_POST['cidrch'])):
shell_exec('/var/www/getStarted -c $ipcidr_text');
$output1 = shell_exec('echo "CIDR Range"');
echo "<pre>$output1</pre>";
elseif (isset($_POST['singlech'])):
shell_exec('/var/www/getStarted -s $ipcidr_text');
$output2 = shell_exec('echo "Single IP"');
echo "<pre>$output2</pre>";
else:
$output = shell_exec('echo "Incorrect input"');
echo "<pre>$output</pre>";
endif;
$testing = shell_exec('echo "testing"');
echo "<pre>$testing</pre>";
?>
PHP is working, I am able to execute a basic script:
<?php
$output = shell_exec('echo "Incorrect"');
echo "<pre>$output</pre>";
?>
If you need anymore information, please let me know. Appreciate any help!
I've had issues with shell_exec() in the past and tend to lean on system() instead, also try " instead of ' to encapsulate the command. You may also need to change how you call your script:
From:
/var/www/getStarted
To (call the python interpreter specifically):
/usr/bin/python /var/www/getStarted

Beginner Help: Creating and Storing Text Using PHP

I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.

Categories