I am using on centos server crontab
* * * * * php -q /var/www/domain.com/public_html/script.php
script.php contents:
<?php
require_once( '/var/www/domain.com/public_html/wp-load.php' );
global $wpdb;
$results = $wpdb->get_var("SELECT post_date FROM wp_posts WHERE id = 70");
$fn = "/var/www/domain.com/public_html/file.txt";
$file = fopen($fn, "a+");
fwrite($file, $results."\n");
fclose($file);
?>
When I go directly to: http://www.domain.com/script.php it executes fine and writes to file, when crontab is want to execute i didnt get writen result from db to file.txt... What is the problem ? it seem wont to load wp-load.php? any ideas ?
Crontab is working, when I comment wp-load.php it writes to file...
Without insight in your system, this might be related to permissions. What user runs this via the web interface, and what user runs this via cron?
In any case, you should put error checking in your PHP script:
$file = fopen($fn, "a+");
if (! $file)
print("Oops, cannot open $file\n");
See e.g. w3schools for an example of error reporting in such things.
Related
I need a way to log queries that go to wp-admin/admin-ajax.php (extra points if I can log specific queries). This will help to troubleshoot some stuff happening in production with my custom plugin.
Scenario
I’ve been working on a plugin which varies the message a user gets depending on the time on the server. The process also depends on other settings retrieved from the server.
The plugin uses javascript to call admin-ajax.php to do the magic. (due to the nature of the plugin, I don’t think I can or should use straight client-side JS for this).
In development it works reliably but in production there are definitely situations where the result returned is unexpected.
This has led me to think that the results of admin-ajax.php are sometimes cached, I need a way to validate my current guess by doing some logging of responses from the production server.
Put this code in you theme functions.php
A log file ajaxlog.txt will now be created in your template folder
add_action( 'admin_init', 'my_ajax_checker', 10, 2);
function my_ajax_checker() {
$file = dirname(__FILE__) . '/ajaxlog.txt';
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$message = $actual_link . " - " . date('m/d/Y h:i:s a', time()) . " - " .$_SERVER['REMOTE_ADDR'] . "\r\n" ;
file_put_contents($file, $message, FILE_APPEND);
}
}
Send requests with a unique identifier and then count if they are all in the log.
These are the steps that I followed to set up my CRON :
define('DISABLE_WP_CRON', true); added in wp-config.php file
Installed WP Control and Added a Hook lead_mail_gun.
Added following code in my code snippets.
add_action( 'lead_mail_gun', 'cron_test_function' );
function cron_test_function() {
$email = 'my-mail-id';
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
wp_mail('my-mail-id', 'WP Crontrol', 'WP Crontrol rocks!', $headers);
}
I can see my funciton in the list of CRON events list.
Also I can see The DISABLE_WP_CRON constant is set to true. WP-Cron spawning is disabled. which confirms that default CRON is off
In my dreamhost I added a CRON Job with the following command /usr/local/php72/bin/php /home/path_to_my_cron_file/wp-cron.php
running every 10 mins.
I am getting no emails. What can be the cause of emails not being sent ?
I have tested that I can send emails though my server.
So the fix was simple.
Use the following in the command section of CRON.
curl https://www.example.com/wp-cron.php
I have some php script that writes an html file to a folder. This works perfectly outside of WordPress. But when I try to incorporate my code into a plugin, it won't write to a folder anymore. I've even tried writing it to the original folder I tested it in at the root directory which is outside of WordPress and it won't write to that either.
Here is the function that writes to an html file.
function buildFrameFile($html, $filename){
$filename= '/wp-content/plugins/my-plugin/html/' . $filename . ".html";
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
return $filename;
}
I've even tried
$fh = fopen($filename, 'a');
$fh = fopen($filename, 'a+');
$fh = fopen($filename, 'w');
$fh = fopen($filename, 'a+');
None of these work either.
I'm thinking that within WordPress it may be something in a php.ini file and or .htaccess files that needs to go in the folder I want to write to, but I'm stuck.
* UPDATE **
I found some errors in the error log and are as follows:
Error: fwrite() expects parameter 1 to be resource, boolean given in....
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
SECOND UPDATE
I added the following code and it now writes the file to the folder
function buildFrameFile($html, $filename){
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$filename= $DOCUMENT_ROOT. '/wp-content/plugins/my-plugin/html/' . $filename . ".html";
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
return $filename;
}
But now when it calls to open the file it try's to open it from the following link
/var/chroot/home/content/##/########/html/wp-content/plugins/my-plugin/html/c413c4976260c1a786e7a48be03f3ad2.html
and I don't believe that link is going to allow the file to be found as it doesn't show up in the browser.
Read your server's error_log and use the error message to diagnose the problem. If you find an error in the log, post it here for a better answer.
Edit based on error messages:
You can try hard-coding $filename into the fopen() function to test if the path to the file is incorrect in your $filename variable.
You'll need to figure out the value of $fh using something like var_dump() if $filename is correct. Most likely the path to $filename is not correct once you pass it in using wordpress through fopen. Instead you're passing in an error message from fopen of FALSE stating that it didn't get any data when it tried to open the file. It's possible that you just need to create a blank file with the proper name wherever wordpress is trying to open it.
This is my source code in which I used SQLite database in my PHP source code. When I run the script it give me this error
Warning: sqlite_query() [function.sqlite-query]: no such table:
books in D:\wamp\www\sqllite\index.php on line 13
Error in query: SQL logic error or missing database
I think the error is at the path of database
<?php
$db = $_SERVER['DOCUMENT_ROOT']."umer.db";
$handle = sqlite_open($db) or die("Could not open database".sqlite_error_string(sqlite_last_error($handle)));
$query = "SELECT * FROM books";
$result = sqlite_query($handle, $query) or die("Error in query: ".sqlite_error_string(sqlite_last_error($handle)));
if (sqlite_num_rows($result) > 0) {
echo "<table cellpadding=10 border=1>";
while($row = sqlite_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
sqlite_close($handle);
?>
The error is probably the path, which is determined by this line:
$db = $_SERVER['DOCUMENT_ROOT']."umer.db";
To troubleshoot this you should try an
echo $db;
And then compare that to your actual path - from there you should be able to adjust your string.
If the path is still correct, double check your file permissions and make sure it is readable by the user that is running the php process.
Good luck!
php will start looking for the database file relative to the directory where the currently running file came from so constructs like("../../databases/mydb.sq3") are usually more portable than using an absolute path or a path relative to DOCUMENT_ROOT as these can be changed at a whim.
Your program is almost certainly not finding the database file, in these circumstances sqlite will create a new empty file, and, causes great confusion by raising a "table not found" error when you try to use the database.
Look for an empty "umber.db" appearing somewhere unexpected and this will give you some indication of where you are going wrong.
I downloaded the latest version of OpenX 2.8.6 and I am trying to setup geotargeting, but it doesn't work. I enabled geoTargeting in administration panel ( Configuration -> Global settings -> Geotargeting module type -> OpenX Max mind (flat file) ). I read in documentation of OpenX that it's not necessary to put any path to the database in plugin settings, so I tried without. I setup delivery options of test banner to be shown only in Serbia. I am refresshing the page that displays banners, but this banner never shows.
I thought that maybe the problem is in old database and that my IP address is not recognised, so I downloaded the latest one database (.dat file) from MaxMind (lite version of Country database) and put the path in settings of the plugin, but it still doesn't work.
Can anyone help me how to resolve this problem?
I had the same problem. It seems that OpenX since version 2.8.x is using its own php-based GeoIP-Database reader (e.g. the "flatfile" option under settings) instead of using a geoip module - which does not seem to work with current GeoIP.dat
To solve this problem I did the following:
1) open plugins/geoTargeting/oxMaxMindGeoIP/oxMaxMindGeoIP.delivery.php
2) search for:
if (isset($GLOBALS['_MAX']['GEO_IP'])) {
$ip = $GLOBALS['_MAX']['GEO_IP'];
OX_Delivery_logMessage('['.$ip.'] : ip from cookie. Plugin_geoTargeting_oxMaxMindGeoIP_oxMaxMindGeoIP_Delivery_getGeoInfo', 7);
} else {
$ip = $_SERVER['REMOTE_ADDR'];
OX_Delivery_logMessage('['.$ip.'] : ip from remote addr. Plugin_geoTargeting_oxMaxMindGeoIP_oxMaxMindGeoIP_Delivery_getGeoInfo', 7);
}
$aGeoConf = (is_array($conf['oxMaxMindGeoIP'])) ? $conf['oxMaxMindGeoIP'] : array();
3) insert below:
$ret = array(
"country_code" => $_SERVER['GEOIP_COUNTRY_CODE']
);
return $ret;
4) save & done
You will find possible return values in the function header:
* #return array An array(
* 'country_code',
* 'region',
* 'city',
* 'postal_code',
* 'latitude',
* 'longitude',
* 'dma_code',
* 'area_code',
* 'organisation',
* 'isp',
* 'netspeed'
* );
*/
Read your module-doc (of mod_geoip) how to get the geo-data from current (or given) IP. In my above example I am using lighttpd 1.5 + mod_geoip (unofficial module). But this fix should also work with apache_note/pecl-geoip/mod_geoip env...
Oh and btw. its of course much faster relying on mod_geoip which caches the db in memory, than doing it all via php on every request (as openx does it).