I am running a php file using shell script like ./first.sh
In first.sh I am running a php file using following command
COMMAND="/usr/bin/php /home/mydirectory/public_html/members/cron/cron.php"
Under cron file I have this code
mail('myemail#gmail.com','fff','aaa');
include_once '/home/mydirectory/public_html/members/wp-config.php';
mail('myemail#gmail.com','fff1','aaa1');
when I am executing .sh file I am only getting first email and not getting second email. Below second email I have written some more coding to fetch wordpress posts but not working. And if I am hitting the cron.php in browser using domain url, it is working absolutely fine.
I have tested so many other stackoverflow answers but nothing is working for me.
Please help me.
If you examine wp-config.php (I suppose this is a WordPress configuration file), then at the ~bottom of the script you will see the line
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
Unfortunately, WP includes a "bootstrap" file directly into config... wp-settings.php includes many other wp files and initializes things etc. So, this must be debugged at Wordpress level. WP ends execution somewhere in wp-settings.php or other include file, so your php cron exits/dies.
Currently, I have not a local wp installation to debug (sorry), but I am 99% sure wp ends execution.
You can hardcode the info you need from wp-config like database host, username etc. into your cron.
Otherwise, you may parse the php file with token_get_all to retrieve the values (I had faced similar problem 2 years ago, unfortunately I have the code in old job office).
http://php.net/manual/en/function.token-get-all.php
Related
I have a new install. Ubuntu 22.04, Apache2, PHP 8.1 and I'm trying to use PHPMailer. My test page with phpinfo all looks good and shows the includes directory /usr/share/php. My form and script are in a subfolder of my document root and it loads fine. But when I fill out the form and the script starts it errors out unable to find PHPMailer. It's only looking in the subfolder the script is in, not the global includes directory. I don't want to put the mod in every form folder and I know it's supposed to work from the includes folder. Is this an Apache or php config issue? Or something I have to specify in the vhost?
Just verifying and closing out with the answer
I found the issue early today. Thanks to bad/tired eyes, I wasn't seeing the leading dot so it was looking for ./usr... Once i fixed that i was able to sort out the Ubuntu method of calling it from the libphp-phpmailer pkg autoload.php. I was able to get the script working with a few code corrections. The only thing I didn't sort out was PHPMailer:ParseAdresses but i only had a few recipients for this form so it wasn't that big an issue. My form is back online and my managers are happy 😊
I have created a PHP file (my-sites-notifier.php) in a WordPress Multisite installation... The PHP file utilizes the WordPress classes (by including /wp-admin/admin.php) in the file. The main script queries the database for (how many posts are in a queue) and sends an email notification if the number of posts in the queue drops below a threshold amount.
So, everything works great if I load the page in a browser, however, I need to set up a cronjob that calls the script. I created a sh file to call the PHP file for this purpose, containing the following.
#!/usr/bin/php
php -d display_errors=on my-sites-notifier-laucnh.php;
So the sh file works executing a simple PHP file with tasks like echo, however, when trying to execute (my-sites-notifier.php), which are dependent on the admin.php include (which itself calls other PHP includes), it doesn't work (and it is kicking back no errors).
Note, (my-sites-notifier.php) is using WordPress commands which make this doable for my low level of expertise. Attempting to build a native PHP file without using the WordPress classes/includes is beyond my skill level. Note, I have placed the sh file in the /wp-admin directory which sits in the same location as the PHP file I wish to execute.
So, the question is 'How do I Execute a PHP File Which Contains PHP Includes - From the Command Line?" If I can get this, all will be good
Problem Description in Brief:
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html file, but does not work when I upload it to my website. Note that I have made sure that all paths are correct, and that the script file has its own php tags, etc.
Problem Description in Detail:
Yes, I am new to PHP scripting, and yes, variants of this question have probably been asked before. The answers to a few of the questions I have read have noted the path of the php script files to be incorrect. I have checked all paths and confirmed that they are indeed correct (including those on the web hosting server). Furthermore, I have been successful in getting the script to work on my local server running Apache2 with PHP5, but have not been successful when uploading it to my website.
Essentially, I am trying to implement a hit counter script which I have acquired from a Stack Overflow post labelled Visitors counter for simple web sites like Vinaora. The code that invokes the php script looks something like this....
<footer>
<!-- Execute Hit Counter Script -->
<?php include($_SERVER['DOCUMENT_ROOT'].'/php/hitcounter.php'); ?>
</footer>
For the likes of me, I cannot figure out why it does not work on the web hosting server. I have tried other combinations of invoking the script like,
<footer>
<!-- Execute Hit Counter Script -->
<?php include('./php/hitcounter.php'); ?>
</footer>
and,
<footer>
<!-- Execute Hit Counter Script -->
<?php include(dirname(__FILE__).'/php/hitcounter.php'); ?>
</footer>
All combinations seem to work on my local web server, but not on the website! Also note that, I have no problem invoking other PHP scripts using other methods (even on the web hosting server), eg.
<form id="form-query" onsubmit="this.checkValidity();" action="./php/contact.php" method="post">
Any advice/suggestions would be appreciated.
Do you get any PHP error?
First of all, you need to activate error reporting.
Put this before including your file
ini_set('display_errors',1);
error_reporting(-1);
PHP should tell you what's happening.
If you don't see anything, change the filename index.html to index.php and try again.
Maybe be you have used "\" in your include path
Wrong:
<?php include 'includes\header.php'; ?>
You should use "/" to work.
Current:
<?php include 'includes/header.php'; ?>
sometimes it might be dues to casing. check if you you uppercase in naming. on some servers Index.php is not equal to index.php
You might try pasting the "include" code directly into the calling code. Maybe it's the included code itself that's misbehaving...
You are doing completely incorrect thing in the first place.
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html
is just totally wrong
There is no such thing as embedding php file within html file (aside from mod_rewrite ...).
For PHP script to be interpreted you must have it (in 99% of cases) with php suffix. This way you allow PHP to distinguish it from regular PHP and send to php interpreter.
Put simply - create this file (a.html):
<body>
abcd<?php echo 'efgh';?>
</body>
and see the result in your browser - use .../a.html
What do you see?
abcd
and not
abcdefgh
On top you always have to have php not the other way around. Solve this or update your question if incorrect.
I have an index.html in my wampserver www directory. On this html, there is a link for a user to upload file. When I hit the link, I select files to upload but instead of the uploadmanager.php which i have tested in my eclipse debugg environment to work, it displays the some part of the code on the web page without doing anything thing. This is not what I expect. Can someone please tell me what is wrong? Thank you.
sound like you are using php-short-open-tags (<? instead of <?php) without enabling this in your php.ini. change your php.ini or use the standart open-tags to solve this.
Are you sure you enabled PHP in WAMP?
Try creating a new uploadmanager.php file directly in wamp/www (or whatever subdirectory) and paste the code from your tested uploadmanager script into the new file. Then try to run it in WAMP.
I think it is a permissions problem. I copied an index.php file into a c:/wamp/www/subdirectory and it only displayed the code. Once I created a new index.php file and pasted the contents of the old file into it, it worked perfectly.
Are you posting to the uploadmanager.php page? Are you getting an error or just seeing the code? Can you post the code from the index.html page that handled the form and the part of the php code you're seeing for us to look at?
Every now and then I have Apache serving the .php as downloadable files instead processing them on the server, but only with random requests.
Some reasons, why this might happen, are
PHP misconfiguration
PHP-files in a directory without execute rights
wrong content type sent
timeout from script execution
In my situation the last bullet is the most dangerous, but luckily it seems to show up only immediately after modifying some of the .php files. I haven't tracked the problem any deeper yet, but it seems to relate some filesystem level operations (as the disk I/O is a bottleneck) and presents itself only in testing env.
I'm working on a program right now that calls the script mail.php located in /var/www/vhosts/company/httpdocs. mail.php is trying to execute require_once dirname(__FILE__).'/../pear/Mail.php' to do an smtp send and the require_once is failing. My PEAR directory's located in /var/www/vhosts/company/pear. I then tried to add /var/www/vhosts/company/pear to the include_path but require_once is still failing.
I decided to take a step back and replace mail.php as a simple script that does file_exists(dirname(__FILE__).'/../pear/Mail.php') and prints the result to a logfile. When I run the script independently, it works fine and returns 1. When the flash program runs it, it's returning nothing. Printing out dirname(__FILE__).'/../pear/Mail.php' returns the same regardless if I run the script independently or if the flash file runs it. I've also tried chmod 777 on the Mail.php PEAR file but that didn't do anything.
Any ideas on what's going on?
I would bet anything it has to do with two things:
1) Flash/Actionscript normally does not access local file paths.
In other words, it probably isn't even executing the file.
As a compiled client-side module it needs an actual web-accessible URL. Part of the problem here is the design itself. Try it with an HTTP request within the actionscript and you will have better results. If you don't have access to the flash file.. well tough beans there.
Now if you are running a mail routine through actionscript? I would say that is a security risk. You are better off having the actionscript pass the routine to an AJAX receiver routine that checks session credentials and then sends mail.
2) CWDUP restrictions on the server.
Depending on certain server configurations, excecutables normally do not have access to filepaths outside their own root. (i.e. an executible cannot call ....\another directory\other file.) Some servers will allow this, but many wont.
You may want to make sure your PEAR directory is in your php.ini path variable. This way you don't need to use CWDUPs in your directory name at all, it will find it in the includes directory. (which is normally how pear modules work.)
So rather than using a buncha dot-dots.. try working down from the top.
$mailpath=$_SERVER['DOCUMENT_ROOT'].'\include\mail.php';
As a last resort, you can try copying the mail.php routine into the same directory and see if that works. If that still fails, then its your include path to PEAR. (as the mail.php is probably calling PEAR functions.)