"Running" a PHP file - php

I think this has to be a very simple question but I am not finding any answers for what I am trying to accomplish.
I have a PHP file which will send out emails. This part works. The thing is, I need this PHP file to "run" every 5 minutes.
My problem is not with the scheduling, I'm fairly certain I understand how to do that.
My issue is with the "running" of the PHP file.
My mind is totally void of any information of how to "execute" a PHP file rather than just make it open in a browser and spit its code everywhere.
I know that this PHP file works because when I am working on it in Dreamweaver and I click the "Discover" link where it says "Dynamically-related files for this document may have changed and should be re-discovered by the server" it executes this PHP file and I get my emails sent.
I've tried going through command line like THIS but all it does is spit out the php code as if I was running it in a browser. Nothing actually sends.
Is there some way to change the way the PHP file itself is formatted that would make it do this?
What is Dreamweaver doing when I click "Discover" that makes it work?
I feel like this should be a simple thing and its insane that I am not understanding it.....

Assuming you installed PHP with default settings, the PHP folder should be in your PATH variable. In which case, all you have to do is run the command php filename.php.
If not, you'll have to direct the command to where PHP is installed, maybe something like "C:\Program Files\PHP\php.exe" filename.php - either way, all you're doing is invoking the PHP binary on the file.

You have options. If it's a web page that you can access through your browser (and that sends the email as you'd expect), you can just request that page via HTTP every five minutes. Personally, I'd do this with cURL (e.g., curl.exe http://localhost/myfile.php). You can find Windows binaries here. You could also use PowerShell or something.
If this isn't a web page, or if you just don't feel like going that route, you can execute the PHP through PHP's command-line interface (CLI), as you've already figured out. C:\path\to\php.exe myfile.php will execute your code and spit the output to STDOUT. If everything is working with this except for sending email—which I'm guessing you're doing through mail—the issue may be with your PHP configuration. By default, PHP looks for the php.ini file in these locations. You can also explicitly tell the PHP CLI what php.ini file to use with the -c option (e.g., php.exe -c C:\path\to\php.ini myfile.php). Of course, you'll need to have everything configured properly in php.ini to send mail. This can be a bit trickier on Windows than on your average Linux machine with sendmail available.

Related

How do I show / run a PHP file in a browser? As if it was a webpage [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
How do I run a PHP file in my browser? I know what the file is for and I know how it looks because I tested it online. But now I downloaded it locally to my computer and I do not know how to run it on my browser. Is there a way maybe to link it to a HTML like you do with JavaScript or CSS to make it work on a web browser also after it is online how will I manage it to work?
If I can not link then maybe I can use iframe to see it in a HTML iframe. Is that possible? Yet the question is not based about iframe, but I do not exclude the iframe possibility too. That is why I ask of it too.
Or even better, if there is a way to both link and use iframe for the PHP?
The entire PHP file I got is a email function that works and looks a loot like a HTML and JavaScript file, but it is written in PHP. If I simply change the name to the file from PHP to HTML I can open it in the browser and it looks like a HTML file with few defects since it is a PHP file after all.
The file I downloaded was from here:
https://www.hscripts.com/scripts/php/registrationMailer.php
And it is this PHP file I am specifically intrested in:
https://www.hscripts.com/scripts/php/HMAD/hmailer.php
NOTE: I am not asking of "PHP code is not being executed, instead code shows on the page" as the other page is asking.
I am asking of "How do I show / run a PHP file in a browser? As if it was a webpage."
You do not need a web server to execute PHP script unless you need to run it using a browser. Install PHP in your operating system and include PHP path in your environment path or variable (depending on what OS) you are using. If you are just using the script for the mailing stuff from your own computer, you don't need a web server like apache, you just need PHP and its cli program installed and you be run php scripts directly. Google the web how on how run PHP command line. Or visit this official documentation link: http://php.net/manual/en/features.commandline.php
You need to download a server, and install it. If you want to go to the trouble, you can get XAMPP, and once it's installed, move the PHP file to the root of your installation (usually c:\xampp\htdocs\ on windows) and then use the url localhost/script.php in your browser.

Can't write log file in Linux using PHP

I'm running CentOS 6.5 on a Google Compute Engine instance which I use for an ejabberd XMPP server. I also have php 5 installed and ejabberd is configured to use a php script to authenticate users.
So far so good - ejabberd executes the script and recieves the correct result from it. The problem is: I want the PHP script to write a log file. So far I've tried:
Writing a file using file_put_contents to /var/log/mlog.log - this didn't work. so I've tried manually creating the file and giving it chmod 777 (for testing). No result - the file remains empty. But - when I execute the script manually using php from terminal the log is written.
Writing to syslog - I've configured php.ini to use syslog and then tried logging. Same result: nothing when ejabberd runs the script, but when I manually run it it works.
Configuring error_log file and using error_log($message). Again, it didn't work.
I came to realize it must be something wrong with the write permissions of the ejabberd user (which runs the php scripts), but even when I set chmod 777 to every file in every option of the above, the log remains empty.
Any hints? What am I missing? (as you can probably tell, I don't have much knowledge in Linux and this is the first time I'm using it in a project)
This may not be the answer you are seeking. I am not much familiar with Linux. There is a KeyLogging php class know as KLogger. You can create logs using this class. It is very easy to use, You have to download php file and use it. You can find it in github. Hope this might solve your problem.

Python to POST data to local PHP on linux

This may sound a bit strange but I need this to happen like this for a reason.
I need python to be able to post data to a local PHP file on a linux server and then get a response from the PHP.
I have tried this:
p = subprocess.Popen("curl --data 'param1=value1&param2=value2' /home/hbmukwm/temp/receive.php", shell=True, stdout=subprocess.PIPE)
reply = p.stdout.read()
But when I do this, as it's a local file I get this curl error: curl: (3) malformed
I also tried it this way (found on a stackoverflow answer)
mydata=[('one','1'),('two','2')]
mydata=urllib.urlencode(mydata)
path='file:/home/hbmukwm/temp/receive.php'
req=urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
reply=urllib2.urlopen(req).read()
But in this case I get the php response as plain text like I'm just trying to read the PHP file, again I think this is happening because the PHP file is local, like this:
<?php echo "test"; ?>
Rather than just
test
Hope this is making sense so far. All I need to do is POST the data to the local PHP file, and then get the PHP response back to python... Is this possible...
Any response is highly appreciated, sorry for my ignorance!
Thanks everyone for your responses, I have learnt something at least! The reason I wanted to have it as a local file is because I didn't want the PHP files to be publicly accessible, though data needed to be sent to them from any location the application was run. So my idea was to create a small python application that runs on a port on the linux server that takes commands and passes them to the PHP files local to the server.
Anyway, the way I have got this working is by using a command line (Like my first example) except using:
p = subprocess.Popen("php /path/to/file 'data=this'", shell=True, stdout=subprocess.PIPE)
And then in the PHP files grabbing the argument like this
$data = $argv[1];
This isn't posting the data to the PHP files like I initially was asking for but I guess I mis-understood what I needed to do. So thank you for all your help guys! I can't really figure out which one of your answers was the definitive answer because you were all correct in what you said, but the answer I am posting now is how I overcome the problem.
Thanks again guys, Stack Overflow is the best :)
You need a web server to run PHP code. You can't just refer to it as a filesystem location. I recommend that you install [WML]AMP web server bundle on your machine which will install Apache, PHP, MySQL and it's easy to setup and configure for beginners. Here are the links for the AMP family. Use the one that
Windows : WAMP
Linux: LAMP
MacOSX: MAMP
You will need a web server to actually parse and interpret the PHP scripts. However, in case you want to do it via the command line this page should give you some details:PHP Command line usage. You have not mentioned which version of PHP you are using, however this appears to be supported since PHP 4.3.0
I guess it is a matter of taste, but I am using "xammp" from www.apachefriends.org
You can easily setup a webserver from there with php5, sql and so on. Wit BitNami you are also able to easily install joomla and other stuff.

php not running on server

My server was running PHP scripts perfectly up until now. I don't know what happen to my server, but the PHP scripts are not running, instead, it is automatically commented by the server. When I uploaded my php script to show PHPINFO, this is what gives me back:
<!--?php
// Show all information, defaults to INFO_ALL
phpinfo();
?-->
if you view the source code, you would see that my php scripts are commented. What should I do to get my PHP scripts running?
Thanks for helping.
Does the same thing occur if you execute the script from the PHP command line interface?
Seems like something external is explicitly disabling PHP execution either at the web server level or software level. Are you using some type of framework around PHP which my be limiting PHP execution, ie CakePHP, joomla etc?

Run a PHP CLI script from a webpage

I have a (possibly dumb) question.
I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc.
The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.
Any ideeas will be greatly apreciated, thanx.
There are many possibilities why exec() and related function calls are not working for you.
Your webhost does not have PHP-CLI installed. Just a webserver module
You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
The webserver user does not have rights to access the php binary
Et cetera..
maybe update the php script to be both an include and a cli script.
use
__FILE__
to check if it's a file, then read the params. otherwise do nothing.
and as an include just call the function you want directly.

Categories