running a php file without calling it - php

How can I run a php file without opening in browsers? Can I do that with php? or Should I use other languages?
i.e. sending a birthday mail to users otomaticly
i.e.2 send a ping every 5 hours etc

for automate your scripts you should use cron

I'd use python for it.
However, PHP works perfectly fine for this purpose. It has the cli SAPI which is meant for commandline scripts (including cronjobs etc.).
Simply start your PHP file with the hashbang line pointing to the PHP interpreter:
#!/usr/bin/php
<?php
echo 'hello shell';

CLI is command line interface. And from cron you can start your PHP scripts through command line.
Try this to start and you will understand what to do next:
http://www.php.net/manual/en/features.commandline.usage.php
Form you command line run something like
C:>c:\php\php.exe c:\path\to\your\script.php

Related

Running PHP Script In Powershell keeps opening NotePad ++ rather than executing the PHP script

Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php

Find out what PHP is running on

I have a script which is running on Apache and also can be executed from command line.
How can i know, on what is php script running, apache or command line?
There is a constant build in PHP that you can use PHP_SAPI. If you are on the commandline the value of this constant is cli than you are on the command line. Every other value like cgi, cgi-fcgi, etc.
Why not add a parameter and only pass it when invoking from the command line?
http://php.net/manual/de/reserved.variables.argv.php
Create a page upload it and browse to it.
See page contents below.
<?php phpinfo(); ?>

Aliasing a PHP CLI script so that the execution doesn't need PHP prefix

I have a PHP CLI script that I invoke using
php application.php --args etc
However I would like to alias the script so that I can just execute the script without prefixing the command line call with php and having the '.php' extension.
application --args etc
Is this possible? I pressume it is but lack the knowledge or probably the correct terms to search for in Google.
You need to do the thing that Mike Brants says add the next line to your sample.php file
#!/path/to/cli/php
but also you have to do these in linux
chmod +x sample.php
To tell the linux (unix) machine to interprete these file as an excecutable
You could just use a shebang to define the application to use for execution from within the file. So at the beginning of your script you would place something like this:
#!/path/to/cli/php
<?php
// start your PHP here
When executed from command line the OS will know to use the specified PHP CLI application to execute the script. Obviously the path to the PHP CLI excutable will vary based on your system and should be substituted with what I have shown above.
This is more flexible that aliasing IMO, as you don't need to enter an alias for each PHP script you may want to run in such a manner from the command line.
ahah. alias can be added to the .base_profile
http://www.hypexr.org/bash_tutorial.php#alias
Use a so called 'shebang':
In the first line of your script add:
#!/usr/bin/php
where /usr/bin/php is the path to your php cli executable.
That's it !
Even more logical and pleasant (at least my favorite) call is #!/usr/bin/env php
Quote part from the user contributed note on PHP manual it self:
uses "env" to find where PHP is installed: it might be elsewhere in the $PATH, such as /usr/local/bin.

Getting cron job error because of <?php tag

I've set up a cron job to run. It executes a php file which is named cronj.php
But it doesn't work and cron job notification I get is:
/root/website/myworld/blabla/cronj.php: line 1: ?php: No such file or directory
And line 1 in that file is simply a php tag <?php I don't know how
Cron is executing the file as if it was a shell script. Normally you would put in a shebang line (like #!/usr/bin/env php) at the top of the file so that the shell knows how to invoke it, but PHP doesn't like it - as it outputs everything outside its tags. Thus, instead of this:
0 3 * * * /mypath/myscript.php ...
try this:
0 3 * * * /usr/bin/env php /mypath/myscript.php ...
or use #Ravenex's trick.
EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. Apparently, shebang does work in PHP. My apologies to #chess007.
We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:
/usr/bin/curl http://www.ourdomain.com/page.php
If I remember right we had some issues using localhost to try to avoid external lookups. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.
Try to call the web url (http://.....).
It's apparently not parsing it as an PHP script.
Edit:
Please show use the cronjob you used, to verify my hunch was right.
Use this to set your cron and also give email address in your cron setting Cpanel so that you get an email when cron runs successfully
wget -O - http://YOURSITE/cron.php

Running a Python script in the background from a PHP file

I need to run a Python script in the background after being called from a PHP file. The PHP file should continue to run independently of the Python script (i.e. it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself).
The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04.
You could use exec() to kick off the Python interperator and have it send its output to either a file or to /dev/null with redirection. Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result.
http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail.
PHP Process Control can be used for this. The proc_open command can be used to start a process. You can later check up on it, read it's output etc.
View the manual entry: http://www.php.net/manual/en/function.proc-open.php and search around google for PHP Process Control
I'm guessing the PHP file is called via Apache, in which case you won't be able to fork(). You should make your Python script daemonize. Check out python-daemon.
You could use:
<?php
shell_exec('./test.sh &');
?>
where ./test.sh should be the execution line to your script

Categories