cron jobs php code in file - php

I have a php script whose name is XYZ.php. When I load that file in browser it executes a form every 5 seconds automatically. I want cron to run this script every hour and I don't have any problem in setting up cron job in cpanel but I don't know how to write php codes in XYZ.php file to work with cron.
In my php file there is one form which executes random name from database every hour and submit it to database once again.
So in form there is only one field i.e name of users which needs to be run from cron.
Please guide how can I do that. If I can open that file for 30 seconds using cron then it will do the work I am looking for.
Edit:
This is my form javascript will automatically submit it in 5 seconds so how to execute this script from cron
<form name="form1" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input name="lastlogin" type="text" id="lastlogin" value="<?php echo (time()); ?>" size="40" />
<input type="submit" name="Submit" value="submit" />
</form>

cron is a tool designed to execute command line programs. You appear to have a JavaScript-based web application: you simply can't use that. While you'd be able to retrieve the page using a command line web browser, the dynamic part would not be executed.
The obvious answer is to write a command line PHP script. There's a full chapter about it in the PHP manual: Using PHP from the command line. I recommend this section:
http://es.php.net/manual/en/features.commandline.usage.php

You don't need a form post at all. Have your script file do the database work when executed. Then have your cron job be:
php XYZ.php
If you want the database work to be done from a cron job and when a user goes to a web page and submits a form, create a class in XYZ.php that does the work. Then have a script called XYZ-cron.php that creates the class and does the work. Then create a script ran by Apache called XYZ-form.php that renders a form and handles POST requests to do the same work, using the class from XYZ.php.

Related

PHP variable used in shell_exec giving different results

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.

Running a PHP in the background while displaying another PHP

Currently I have 2 PHP files. 1 is the user interface and another fetches data from the backend and inserts into the database.
Currently if I use the following in the UI php:
<html>
<body>
<form action = "test.php" name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit"/>
</form>
</body>
</html>
Upon clicking submit it goes to the test.php. If it possible to execute test.php in the background while remaining on the UI php?
Some of the previous posts talk about using ajax etc which I am not sure how to implement. Possible to do this in php?
In test.php you can use the exec function and call whatever php file you want using php by command line:
exec("php test.php > /dev/null 2>/dev/null &");
Just notice that the session that it will have is not the same as what you have on the browser, and it can be tricky to send parameters to the command line instance that is being initiated, take a look here.

PHP that accepts file upload from a single HTTP POST

I'm trying to figure out how to get a server to accept a file through HTTP post in one step - without going through all the trouble of creating an html form and clicking the submit button.
I know how to write a PHP/HTML package that accomplishes the following:
user points their local browser to a URL with an upload form
user selects a local file and clicks "upload" button on the form
server accepts that file and places it in a specified place
I create an HTML file with a form that calls a php script when the submit button is clicked.
I'd like to change this to the following:
From command line, user executes the following command:
"curl -X POST #somefile http://myhost/getter.php"
server (myhost) accepts that file and places it in a specified place
Said another way, I'd like to send the file directly to the php script without going through the form step.
Much thanks for any guidance.
http://curl.haxx.se/docs/httpscripting.html
4.3 File Upload POST
<form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
command line equivalent:
curl --form upload=#localfilename --form press=OK [URL]

Need code to execute a single php script that exists on multiple servers (without leaving the calling page)

I've got an updater.php script located on several of my sites. This updater.php file is set to execute code when called from a home base (my central server).
So I'm looking to create a dashboard of sorts in which all my remote site addresses are listed, with the path to this updater.php script like so...
www.server1.com/path/updater.php
www.server2.com/path/updater.php
www.server3.com/path/updater.php
...etc (there will be lots of them)
And I'll create an interface to list those along with checkboxes beside each one, and a select all, etc
And I'm looking to create a PHP script that will iterate over the whole collection of urls in that list and execute the call to the updater.php file on each server, passing it a "version=v001" for example...
$.get("http://server1.com/path/updater.php?version=v001");
$.get("http://server2.com/path/updater.php?version=v001");
...etc
I've already set up the code in updater.php (the file that resides in all my sites) so that when it receives a request, it parses the $_GET['version'] to see what the version is and it knows which file to go get on my central server to perform the update.
I'm just looking for some clues how to create the script for this dashboard that sets it all into motion...
PS: In total, this is basically a batch updater script that executes wordpress theme updates without having to go to each site and do them individually.
It looks like you're using jQuery and that AJAX is fine to call the scripts rather than a PHP script. If that's not the case, ignore this.
When your "GO" button is clicked, use javascript to get the values of all the checked checkboxes. Loop through those, doing the $.get() thing. That should be all you need to do.
Optionally, you could catch the responses from the get() calls and update a status div to let you know whether they all ran successfully.
You might wanna check PHP's cURL extension which allows you to send request to multiple sites within your script quite easily.
If you need further assistance, let me know - I'll edit.
EDIT
You could use one of the javascript frameworks that would allow you to easily manage your AJAX calls (i.e. jQuery - imho the most stringent). Then do something similar to:
<form name="updater" id="updater" ...>
<input type="checkbox" name="server[]" value="0"/> Server 1
<input type="checkbox" name="server[]" value="1"/> Server 2
...
</form>
<script type="text/javascript">
var server_url = [
'www.server1.com/path/updater.php',
'www.server2.com/path/updater.php',
'www.server3.com/path/updater.php'
];
$('#updater').bind('submit', function() {
$('input[name="server[]"]:checked', this).each(function() {
$.get(server_url[this.value]);
});
});
</script>
Here you go!
I don't see the need for javascript. Honestly, I would just dump out a bunch of iframe tags which point to the appropriate urls if you're shaky on relying on php's abilities. Let the browser do the requests. Nice thing about the iframes is that you can have your remote scripts output "success" or error messages.
You can't use jQuery to do this alone because of the in-ability to do Cross Site Scripting.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// Run through each
foreach($_POST['servers'] as $serverHost)
{
// Now contact the server.
// if getting urls is disabled (which some hosts do)
// you can use CURL to connect to the server.
$result = file_get_contents($serverHost);
}
}
?>
<form method="post" >
<input type="checkbox" name="servers[]" value="http://foo1.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo2.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo3.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo4.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo5.com/updater.php" />
<input type="submit" />
</form>

Form calls 2 php functions

I have an html form which uses a PHP file to submit data by email. I want to add some code (which I already have) to generate random numbers for spam protection. Can I call another PHP file within my form?
Here is the code that goes in the form:
<form name="mail" action="go.php" method="post" onsubmit="return CheckData()">
<input type="text" name="q">
<input type="submit" name="Submit" value="OK">
</form>
I am a real novice with PHP so any help would be appreciated.
LozFromOz
you can do it with image in your form that call to php file.
the famous is to use captcha,
read this link :
Stopping scripters from slamming your website hundreds of times a second
a good captcha to insert in php :
http://recaptcha.net/plugins/php/
There's no need to have the browser make two http requests for two different urls to the webserver. Your php script go.php can do what ever you want it to do, e.g. include two other scripts and/or calling two functions or ...
<?php // go.php
require_once 'spam_protection.php';
require_once 'form_helper.php';
require_once 'database_something.php';
....

Categories