Cpanel cronjob not working - php

I have a php script in root of my site. I have added a cronjob in my cpanel.
It is working with basic database operations like shown below:
<?php
require_once "classes/class.database.php";
$db = new database;
$db->connectToDB();
$data = date("Y/m/d H:i");
$res = $db->insertRow("cron",array("datetime"),array($data));
echo $res;
?>
In same file I have replaced these codes with codes below which are real codes that I want to schedule but it is not working. If I enter manually, it works but by this way it doesnt working.
Real Codes:
<?php
require_once "/home/domain/subdomain.domain.com/share/share.php";
$share = new share;
$share->sharePosts();
?>
I dont think there is an error in my code because it works manually however I want to be sure about that. Can I log output of this file?
Thanks in advance.

I have finally solved the issue.
I have enabled error logging with codes below.
error_reporting(E_ALL);
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
I have tried to mail myself (it is cpanels property) and just echo something.
Mail came succesfully. Then I wrote wrong code (I wrote wrong path to "required_once"), when I execute manually, gives fatal error. Fatal errors not mailed. After that I replaced "require_once" with "include" to avoid fatal errors, then it noticed an error but this time it mailed to me. Error was "No such file." Then I tried path like that "/home/domain.com/yourcron.php" and no errors. In conclusion, all paths must be like "/home/domain.com/yourcron.php".

Related

#date_default_timezone_get() silently crashes

I have the following test code:
<html>
<body>
<p>
Hi
</p>
<?php
if (function_exists('date_default_timezone_get')) {
echo "J";
$timezone = #date_default_timezone_get();
echo "K";
date_default_timezone_set($timezone);
echo "L";
}
phpinfo();
?>
</body>
</html>
When I retrieve it through my apparently working apache I get just:
<html>
<body>
<p>
Hi
</p>
J
If I comment out the '$timezone = #date_default_timezone_get();' then I get a response with an error about '$timezone' not being defined and then the standard phpinfo() output.
All of the debugging options I've found thus far don't show me anything of interest in any log.
Running the script with php on the command line it gives no error either.
FYI: This is me attempting to figure out why my websvn suddenly stopped working, I've narrowed down the failure to this line and it fails in the sample code, so I'm feeling reasonably good about this other than why it just wont work.
Why are you keeping the # before the function call? It suppresses the error output which may be of help.
Also, what PHP version are you using? DateTime functions are only enabled by default since 5.2.0.
I wouldn't also exclude the possibility that even though date_default_timezone_get() function exists, it is not a standard one that comes with PHP, but something custom-made instead.
After commenting out the call entirely in websvn I got a new error from another line:
Timezone database is corrupt - this should never happen!
This error led me to investigate what was wrong with my timezone info and apparently all of files in /usr/share/zoneinfo/ had 640 permissions, changing them to 644 and the errors have gone.

file_put_contents stops my code from executing

I want to send some data as a GET request to my php page (submit.php) and save it in a local file. I have:
$loc = 'data.txt';
if(isset($_GET["data"])) {
echo 'set';
file_put_contents($loc, $_GET["data"], FILE_APPEND);
}
echo 'foo';
But when I access submit.php?data=bar, nothing happens to data.txt; moreover, echo 'foo' does not seem to execute. Why is this?
echo 'foo' does not execute because file_put_contents() encounters an error and the execution stop.
Put error_reporting(E_ALL & ~E_NOTICES); ini_set('display_errors', '1'); in front of your script to let PHP display the errors on screen.
This way you can find that, I guess, the process that runs the PHP code (the web server probably) does not have the rights to write in the directory where you store the code.
Change $loc to '/tmp/data.txt' and it will work.
Or, even better, create a new directory, set its permissions to rwx for everybody and change the code to write files in it.

importing files in php using require() not working

i tried the following code to import two files
<?php echo "php";
require('../globalvasr.php') or die("error");
require('../newcosn.php') or die("error2");
$config = new GlobalConfigs();
?>
It does not shows error and it just simply displays a blank page.Also i am unable to use the variable defined in those two files.
Like $config->DBNAME.
I dont know whats wrong in this.
Please help me find it.
Thank you.
require, in contrast to include, automatically dies and does not have a return value.
This means the or die() is bad. Better:
<?php echo "php";
require('./globalvasr.php');
require('./newcosn.php');
$config = new GlobalConfigs();
?>
Require generates a fatal error when require fails, causing the script execution to stop immediatly.
As you seem to be running in a web env, your output (all echo or print statements) is buffered until the end of the script.
So here the require fails, causing a fatal error (that should be available in the error log) before the output buffer is emptied, preventing your first "echo" to be sent to the browser. that's why you get a blank page.
Try replacing the require with an include, you will get a warning instead of the fatal error.
if you have blank page, set error_reporting: E_ALL and Display_errors: On in php.ini or put on start of your script these lines
error_reporting(E_ALL);
ini_set('Display_errors','On');
then you will see errors

How to use KLogger?

Common handler i used
<?php
function error_msg($err_type,$err_msg,$err_file,$err_line)
{
$fh=fopen("error/errorlog.txt","a");
$date1=date("Y-m-d H:i:s");
$er="
===============================================================================================================
"."
Error: Type: ".$err_type."Message: ".$err_msg."ErrorFile: ".$err_file."Errorline: ".$err_line."Time: ".$date1.
"
===============================================================================================================
";
fwrite($fh,$er);
fclose($fh);
}
set_error_handler("error_msg");
?>
These codes Log error perfectly. Since i am using framework i cant use this codes. so i am using KLogger. KLogger perfectly logs my error but it also display error in front screen to user.
How to log error using KLogger If any one use this KLogger Help me how to use with simple examples.
Just do something like:
require_once 'KLogger.php';
$log = KLogger::instance('/var/log/');
$log->logInfo('Returned a million search results');
$log->logFatal('Oh dear.');
# Output will log to the path you specified, at log_[current-date].txt
It's crazy simple. Read the docs at GitHub
PS, I wrote KLogger.

PHP voting code works on 5.2.5 but not on 5.2.11 anymore

Ok, so a little while back I had some help writing some PHP voting code, it worked just fine after I upgraded my server to use the latest version of PHP. However now I have switched servers, and the PHP isn't as up to date as the other one. Anyways here's my code:
<?php
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}
if($_GET['click'] == 'up1'){
file_put_contents('vote/1u.txt', ((int) file_get_contents('vote/1u.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
Execute and display:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('vote/up1.txt'); ?>
Now when on my other server (PHP version 5.2.5) this code worked great! However on my new server the PHP version is 5.2.11, and because of this the code won't work. My question is, is there any way to make this more compatible with an earlier version of PHP, or to write completely new code that will work just like this one? Or is there a way to tell my servers to use PHP 5.2.5+? I'm using cPanel X admin panel.
I have set the text file permissions to 777 and still nothing!
you are checking for variable "click" but executing the code only if it equals "up1".
But your link tells click to equals "yes" so that part of the code is never true, hence never executed.
Change your executor to this:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('counteru.txt'); ?>
But more logically, your processing code should be rationalized a bit to this:
if the link is clicked :
First, if the data file (lu.txt) does not exist, create it and write '+1' inside of it, else, add 1 to its existing value.
Then, redirects to the initial page.
if($_GET['click'] == 'up1'){
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}else{
$content = file_get_contents('vote/1u.txt');
if(!$content){
die("Error! file_get_content failed !");
}
file_put_contents('vote/1u.txt', ((int)$content) + 1);
}
header('Location: ' . $_SERVER['SCRIPT_NAME']);
}
exit;
Not a bad idea to add a trim() around file_get_contents(). Or to check if $_GET['click'] isset() prior to checking if it's equal to 'up1'.
It's conventional to exit() instead of die() after a header redirect--well, from what I've seen at least.
Basically, during development, turn on error reporting and set your error flag to E_ALL to see everything, including warnings and notices--neither of which halt your code, but should still be known and addressed.
You might discover the reason your code produces different outcomes under different minor versions of PHP by turning on full error reporting.

Categories