PHP variable used in shell_exec giving different results - php

This is a repost because my question before was not clear enough and deleted it to prevent further confusion.
I have a shell script that takes one argument. It uses the argument to cURL a website which then uses grep, head and ${variable#*>} to extract some data from a website.
In terminal everything works well with the command below. $stockCode being the argument. ./priceAlert.sh $stockCode
I tried to make this into a working webpage on my local host. (MAMP osx) I tried different solutions but the website will always spit out "not from same origin" except for the shell script.
After searching for how to integrate shell scripts with web I came across PHP and now I am figuring out how to get PHP to talk to my shell script.
Structure of my project:
HTML page with an input inside a form
<form action="priceAlert.php" method="post">
<input type="text" name="stockCode"></input>
<input type="submit" name="submit" value="Submit me!">
</form>
A PHP page that receives the input from the HTML page via POST
$stockCode2 = $_POST['stockCode'];
echo $stockCode2;
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
Now the interesting part...
The value of $stockCode2 from the HTML form is 033360.
If I use $stockCode2 as POST, the shell script extracts the part of the website that I do not want. (The code above extracts the part I do not want)
If I hard code the value, 033360, into $stockCode2 the shell script extracts the part I want. The code below extracts the part of the website I do want.
$stockCode2 = '033360';
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
If I echo everything they all show the value 033360 but behave differently.
Is there a way to fix this? Could this be a way for the target site to prevent scraping?
Thank you.

Related

How to use wkhtmltopdf with POST data

I currently use wkhtmltopdf where I am attempting to generate a .pdf file after a user submits a form on our website. The form data gets submitted to post.php, where it displays nicely as a formatted web page. I want to generate a .pdf file of this exact page.
But the problem begins when trying to execute wkhtmltopdf. I get a continuous loop because I'm trying to generate the .pdf from inside of this post.php file, which is also the target.
I have also tried to include() a separate PHP file to handle the exec() function, but I still get a continous loop.
Maybe a visual below helps:
form.php which contains something like below...
<form id="ecard" action="post.php" method="post">
<input type="text" name="ecard_message" />
<input type="submit" />
</form>
post.php which holds the posted data and contains HTML like so...
<div class="content">
<?php echo $_POST['ecard_message']; ?>
</div>
<?php exec("wkhtmltopdf http://example.com/post.php ecard.pdf"); ?>
My code DOES work when the exec() function is runs separately from these files, but how would I accomplish the exec() within this same process, automatically?
Any insight is very much appreciated.
calling wkhtmltopdf http://example.com/post.php ecard.pdf will lose the post data, so even if it worked, the pdf will be empty.
Rather generate the pdf as html and then pass it to wkhtmltopdf. Eg: (untested)
<?php
$html = '<div class="content">' . $_POST['ecard_message'] . '</div>';
exec("echo $html | wkhtmltopdf - ecard.pdf");
?>
See Is there any wkhtmltopdf option to convert html text rather than file? for explanation of using text instead of files.

PHP File not printing HTML form content

I am taking an HTML5 Tutorial on YouTube. Part of the course discussing CSS3 and PHP.
In the PHP section, the instructor provides the following code to print the form input to the screen:
<?php
$usersName = $_GET['name'];
$usersPassword = $_GET['password'];
echo 'Hello ' . $usersName . '<br>';
echo 'That is a great password ' . $usersPassword . '<br>';
?>
The HTML it refers to is:
<form method="GET" action="phpscript.php">
Your Name: <input type="type" name="name" size="50" maxlength="50"><br>
Your Password: <input type="password" name="password"><br>
Your history:<br>
<textarea name="history" rows="20" cols="50">Just random words</textarea>
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset">
</form>
The PHP variables do not print to the page. This is the output:
echo 'That is a great password ' . $usersPassword . '<br>';
I double checked what the instructor had in the video, and it is exactly the same. Then, I opened the Console in Firefox and I saw an error on the PHP page that said that the doctype and character encoding was not declared. In the instructions, the instructor has a php file with only php code in it. So, I got rid of those two errors by using my html file as a template and adding the php code between my <main></main> tags, but got the same output. So, I added <script type="text/php"></script> around the php code, then nothing printed.
Please help me so I can continue moving forward in this tutorial. The instructor was using a Mac and Chrome and running the files locally. I am running everything locally in Windows 7 and Firefox. I tested things out using IE and Chrome but got the same results.
Thank you!!
Sounds to me like either you do not have php running anywhere (Since it is server side you need a server to run it and I recommend XAMPP for this use) or your file has the ending .html but you would need a .php to run php code. Just putting it in between the <main> tag won't work.
You want to output the script without running it?
Try <xmp></xmp>
For example, if you want to print a link like this, it will look like this:
Google
Google
but with the <xmp> tag, it looks like this:
Google
For just startup you don't need a fully fledged web server , you can use PHP's inbuilt server simply go in php's installation directory save your php then bring a command prompt on this directory and run
php -S localhost:80
Now access file using http://localhost/file.php
You need to keep the shell open as long as you are using php

HTML form button to run PHP to execute Python script

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.

shell script executed in HTML/PHP

I have some basic knowledge of HTML/PHP. The situation I am facing is frustrating. What I want to accomplish is to create a simple search box on a web page, when the user puts in input and clicks submit then my shell script is executed and then presented on a php page. I have been successful in getting other commands to run when I click submit to make sure the PHP exec shell command is working.
I will see the output on the web page. Just not my script. My script uses an argument to pass and works thru command line. Below is the details of my script, HTML, and PHP page. Also, I'm using a FreeBSD 10 box.
My Script
Command Line -
$ csearch "argument"
#!/bin/sh
grep -ir -B 1 -A 4 "$*" /usr/local/var/rancid/CiscoDevices/configs
My HTML page
<html>
<body>
<form method="POST" action="csearch.php">
<input type="text" name="searchText">
<input type="submit" value="Search">
</form>
</body>
</html>
My PHP page
<?php
$searchText=$_POST['$searchText'];
?>
<html>
<?php
$output = shell_exec('/usr/local/bin/csearch $searchText');
echo "<pre>$output</pre>";
?>
</html>
Any help is greatly appreciated.
shell_exec('/usr/local/bin/csearch $searchText'); This isn't what you expect it to be:
<?php
$searchText = 'foobar';
$cmd = '/usr/local/bin/csearch $searchText';
echo $cmd;
?>
outputs:
/usr/local/bin/csearch $searchText
Change the string to use double quotes and $searchText will actually be what you want to it be:
$output = shell_exec("/usr/local/bin/csearch $searchText");
More info on the use of quotes in PHP.
As #uri2x hints in a comment:
$searchText=$_POST['$searchText']; should be changed to $searchText=$_POST['searchText']; for a similar reason.

PHP related files only work when launched from dreamweaver

I am starting to learn php, I made a form that uses a php file to write the information into an xml file. The form is in an html file, and the php action in a seperate file. I am using EAsyphp 12.1
Whenever I use the upload form publishing it through dreamweaver everything goes fine.
But when I open it directly into the broswer it shows me part of the php code.
This is the code for html
<form enctype="multipart/form-data" action="chng.php" method="POST">
<div>Name <input type='text' name="tf" /> </div>
<div>image <input name="uploadedfile1" type="file" /> </div><br />
<input type="submit" />
</form>
The php is
$target_path4 = "../img/";
$target_path4 = $target_path4 . basename( $_FILES['uploadedfile4']['name']);
$name4="../img/linkimagen.jpg";
$t = $_POST['tf'];
if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $name4)) {
$xml = simplexml_load_file('info.xml');
$xml->names[0]=$t;
file_put_contents('info.xml', $xml->asXML());
$xml->images[0]=$shortname;
file_put_contents('info.xml', $xml->asXML());
}
?>
The xml information is then picked up by another page using Jquery ajax.
Everything works fine if I launch the form file using dreamweaver as I said before. Also, sometimes the page that displays the information from the xml will display it if opened directly from the browser, other times it won't, however it will always display if opened from dreamweaver. I've found information for php not working in dreamweaver but not the opposite, as is my case. Is there anything I have to do so it works always? Thanks for any info!
Well if you upload it to your server - there is a php parser which parses your file
if you open it local it won't get parsed so it's just another text document.
Of course you could install something like xampp, to have a local webserver but if you are just starting out - it's a bit much I guess :)
-- Edit --
An addition: Please watch the behaviour of your browser. If you have already a webserver installed on your computer it has an adress which usually is something like h ttp://localhost/ or h ttp://127.0.0.1 - if your file doesn't get parsed it's probably because it opens directly like C:\programm files\dreamweaver\whatever\index.php and not
h ttp://localhost/index.php
hope this helps you ;).

Categories