Hi is it possible to use sessions in a cronjob?
The Script I use is:
session_start();
if(empty($_SESSION['startwert'])){$startwert = 0;}
else {$startwert = $_SESSION['startwert'];}
if(empty($_SESSION['zielwert'])){$zielwert = 10000;}
else {$zielwert = $_SESSION['zielwert'];}
....
$_SESSION['startwert'] = $zielwert;
$_SESSION['zielwert'] = $zielwert + 10000;
echo "Startwert: ".$_SESSION['startwert']."<br>";
echo "Zielwert: ".$_SESSION['zielwert']."<br>";
But the Cron allways start set "startwert" to 10000 and "zielwert" to 20000 and it does not increase the values.
Ok now I have tried this.
/usr/bin/wget -O - http://mydomain.com/script.php
But the cron starts allways with 10000 and 20000. Any ideas?
If you're invoking the PHP script from cron via wget, use the --save-cookies option; if via curl, use --cookie-jar. (If you're invoking the PHP script via php -f [...] or similar, then you'll first need to invoke it via wget or curl instead.)
For example:
wget --load-cookies /tmp/cron-session-cookies --save-cookies /tmp/cron-session-cookies --keep-session-cookies [...]
or
curl -b --cookie-jar /tmp/cron-session-cookies [...]
wget by default doesn't save session cookies, which you want it to do, hence the --keep-session-cookies option; curl by default does save them, so all that's necessary is -b to enable cookies and --cookie-jar to tell curl where to find them. In either case, replace the [...] with whatever options and arguments you're already passing to the program, and adjust the location of the cookie jar file to taste.
Not really. PHP sessions are dependent on cookies (ignoring trans-sid mode), which really only exist in an HTTP context. cron jobs are running in CLI mode, so there's no http layer to deal with.
You CAN force a CLI script to use a particular session file by setting the session ID before calling session_start();, but there's no guaranteed that particular ID would actually exist when the cron job starts, as some other PHP instance's session garbage collector may have deleted it.
Related
So, guys, I have the following code:
<?php
$url = "http://example.com/filetorun.php";
$time = date('g:i A d.m.Y', $res['sometime']);
echo `echo "wget --spider {$url}" | at -M {$time}`;
?>
The main idea is to wget some url at the specific time to perform some useful operations for me.
What happens in the terminal?
When I run this bash code. It works well giving me the output like this:
warning: commands will be executed using /bin/sh
job 59 at Mon Jan 13 17:12:00 2020
What happens in /var/log/apache2/error.log when I run my php script?
It gets this output.
What's about atq?
I can see the job there only when I create it via the Terminal. When I create It from the php-script, I can not do that.
What have I tried to do?
I added users to /etc/at.allow and deleted them from /etc/at.deny
Not a lot of detail to go on, but I direct your attention to this requirement of at:
The value of the SHELL environment variable at the time of at invocation will determine which shell is used to execute the at job commands. If SHELL is unset when at is invoked, the user's login shell will be used; otherwise, if SHELL is set when at is invoked, it must contain the path of a shell interpreter executable that will be used to run the commands at the specified time.
Does your web server user have a login shell defined? getent passwd or grep /etc/passwd to determine. Regardless, I suggest setting the shell to use in the command itself, to prevent issues where someone chnahes the default shell of the invoking user.
If this is not the issue, we probably need more detail.
So I want to execute a bash command from PHP on my web server. I can do this using shell_exec. However, one of the commands I want to execute is curl. I use it to send a .wav file to another server and record its response. But when invoked from PHP, curl doesn't work.
I reduced the error to the following small example. I have a script named php_script.php which contains:
<?php
$ver=shell_exec("curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver");
echo $ver
The curious thing is that when I run this php script from command line using php php_script.php, the result I get is
Status: 500 Internal Server Error
Content-type: text/html
However, if I run curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver directly, I get the response I was expecting:
verdict = authentic
(Edit:) I should probably mention that if I put some bash code inside the shell_exec argument which does not contain curl, the bash command executes fine. For example, changing the line to $ver = shell_exec("echo hello > world"); puts the word "hello" into the file "world" (provided it exists and is writable). (End edit.)
Something is blocking the execution of curl when it is invoked from PHP. I thought this might be PHP's running in safe mode, but I found no indication of this in php.ini. (Is there a way to test this to make 100% sure?) What's blocking curl and, more importantly, how can I bypass or disable this block?
(And yes, I realize PHP has a curl library. However, I prefer to use commands I can run from the command line as well, for debugging purposes.)
cheers,
Alan
The reason is the administrative privileges when you run the command directly you are running it as root and thus the command gets executed. But, when you run the command through PHP it runs as an user. By, default user has not the privileges to run the shell_exec commands.
You have to change the settings of shell_exec through CPanel/Apache config file. But, it is not recommended to provide the shell_exec access to the user as it help hackers to attack on server and thus, proper care should be taken.
It would be more appropriate to use the curl library provided in PHP.
There is a PHP script I would like to execute by a cron-job but it has to be executed every seconds or every 2 seconds.
(PHP file updates the cover photo of a page via opengraph)
So I decided to write a shell script which is below. But how does php act when executed by a shell script, is it works normally like requested from a browser or what happens ?
Does sessions works ?
#!/bin/bash
while true; do
/path/to/file.php
sleep 1
done;
echo "Stopped" | mail -s "Cron script has stopped." mymail#domain.com
The difference is that you won't have $_GET, $_POST and other http specific stuff available. Sessions also won't work (why would you need them?). You obviously won't be able to set cookies, headers and other such things.
Other than that you can pretty much ignore the fact that you are "in a shell".
See here for more details: http://www.php.net/manual/en/features.commandline.differences.php
Take all the browser aspects away from PHP and that's how it'll act. You get no sessions (unless you were to call it with cURL and store the cookie somwehre).
You get no $_GET or $_POST - instead you need to use $args - but if you're not passing in any variables this isn't really relevant to you.
You'll obviously need to ensure that you've given your script execute permission, otherwise, call it using /path/to/php /path/to/file.php.
Take a look at these resources, they should help you out.
http://www.php.net/manual/en/features.commandline.differences.php
http://www.php.net/manual/en/features.commandline.php
When you execute php from the command line (CLI) you enter another environement.
There are multiple differences
Some of them are
Configuration
When executing from command line, you run another php.ini file. (Usually /etc/php5/php.ini)
Permissions
On the web, you usually execute PHP with www-data user.
When executing from command line, script will be executed as the current user.
Arguments
You do not have access to $_GET, $_POST, $_FILES... superglobals anymore.
Instead, you have new superglobals so as $argv and $argc
Sessions
You do not have access to session nor $_COOKIES
Relative Paths
The path that the script uses is relative from where the command started
So, careful when using relative ./ path in your script
Eg:
$ pwd
/path/to/project/
$ cat app/script.php
<?php
echo getcwd(), PHP_EOL;
$ php app/script.php
/path/to/project
$ cd app
$ php script.php
/path/to/project/app
You can get the current directory by using getcwd
Currently I have a parser.php which loads an xml file and inserts new data from the xml file into a mysql database. How would I go about refreshing this php file every 30 seconds so my mysql table always has fresh data? I think I could use short-polling to do this, but I'm guessing this is not the most efficient of options.
Thanks in advance
This is a non-PHP solution which will require you to have shell (SSH) access in order to run the script, however you can also run it through PHP with exec() if you want to. Shared hosting environments might present a challenge for this approach but as long as you can execute scripts under your user credentials you should have no problems running it.
First you will need to create a bash script with the following content and save it (I'll use the name parser.sh for the purpose of this example). You can then adjust the timeout in the sleep 30 line if you want to.
#!/bin/sh
while true
do
php parser.php
sleep 30
done
In order to run the script you'll need to give it execute permissions.
chmod +x parser.sh
Now you can use the nohup command with the ampersand (&) argument to ensure that the script will run in the background even when a termination signal is sent after, lets say, closing the shell (SSH). The ampersand is important!
nohup ./parser.sh &
Now you can use top or ps aux | grep parser to ensure that the script is running. As I already said before you can also use PHP exec() to start the process but shell is still the preferred and most reliable way to do this.
If you want to stop the background process which executes your script then you'll simply have to kill it. Just use ps aux | grep parser to find out the PID of the parser process (its in the second column to the left) and use it with the kill command.
kill 4183
You need to use a cronjob, but crons jobs runs every 1 minute or more.
Another way is to make a "daemon".
Very basic example:
<?php
while(true) {
// check if 30 seconds has passed.
// then execute some function...
};
?>
Then you need to execute this in your terminal:
$ php script.php &
This link should help.
Greatings!
I'm trying to script bash to do a few simple curl commands.
I can generate a cookie that I want to use with the following command:
curl -c cookie --data "user=user&password=pass" //example/login.php
However, when I try passing it into the site again the cookie is ignored. It's as if I didn't even login into the first place. The command I'm using is the following:
curl -b cookie //example/
What am I doing wrong?
Any help would be appreciated thanks.
it turns out I wasn't generating my cookie correctly after all.
I was actually missing some additional variables that were required in my POST statement, which interacted with the php log in script I was using.
You may want to store the cookie that comes back. You do so by specifying a cookie file:
curl -c cookies.txt -d "user=user&password=pass" //example/login.php
and to use those cookie in later requests you do:
curl -b cookies.txt //example/