I am using the following PHP code to get the upcoming date of a given day.
For example If the day given in "Tuesday" it will output 01-09-2020.
The function is to show upcoming sale dates of smartphones.
$saledate = "Tuesday"
date_default_timezone_set('Asia/Kolkata');
function dayDate($day) {
return new DateTime('next ' . $day);
}
$nextsale = dayDate($saledate);
if(!empty($saledate)) {
echo "Next Flash Sale on $saledate ";
echo $nextsale->format('d-m-Y'); // 01-09-2020
}
The problem is if the $saledate is empty, it causes the entire web page to not load. What could be the issue ?
This is what the errorlog says
[25-Aug-2020 18:47:15 Asia/Kolkata] PHP Fatal error: Uncaught Error: Call to undefined function dayDate() in /home/customer/www/sitename.com/public_html/index.php:1114
Stack trace:
#0 {main}
thrown in /home/customer/www/sitename.com/public_html/index.php on line 1114
Update: Fixed by adding the $nextsale = dayDate($saledate); inside the if block.
I need to hide "uncategorized" from the list of selectable post categories and wrote some code to achieve that. This function worked fine up to WP 4.7.5, the categories were successfully hidden:
add_filter('list_terms_exclusions', 'myproject_hide_uncategorized',1);
function myproject_hide_uncategorized( $exclude_query ) {
if (function_exists('get_current_screen')) {
$currentScreen = get_current_screen();
if ($currentScreen->base == 'post') {
$newquery = "";
$excluidos = array();
$excluidos[] = get_category_by_slug('sem-categoria')->cat_ID;
$excluidos[] = get_category_by_slug('uncategorized')->cat_ID;
foreach ($excluidos as $excluido) {
$newquery .= " AND t.term_id <> '$excluido' ";
}
$exclude_query .= $newquery;
}
}
return $exclude_query;
}
However, on later versions (4.8.1 being the latest so far), whenever I try to edit a post or create a new one (visiting `localhost/wp-admin/post-new.php"), I get the following error message shown:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /var/www/html/wp-includes/class-wp-term-query.php on line 294
Raising the memory limit just gets me a timeout error. I am able to replicate the behavior in clean installs of both WP 4.8 and 4.8.1
It looks like the crash is caused by these four lines, adding it to the functions.php file of any theme seems to cause a crash:
add_filter('list_terms_exclusions', 'this_will_crash_wordpress');
function this_will_crash_wordpress( $exclude_query ) {
get_category_by_slug('uncategorized');
}
What changed between 4.7.5 that causes this error? How can I make it go away? Do I have to rewrite my code? If so, how?
try to use get_term_by(); https://codex.wordpress.org/Function_Reference/get_term_by
like this
get_term_by('slug', 'uncategorized', 'category');
I'm trying a simple example
$sftp = new phpseclib\Net\SFTP(FTP_ADDRESS);
if (!$sftp->login(FTP_USER, FTP_PASS)) {
$logger->error("FTP credentials error");
$logger->error($sftp->getLastSFTPError());
} else {
$logger->log("Connection successful");
}
print_r($sftp->rawlist());
However the script just hangs and finally times out
( ! ) Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\XookCatalogFeed\lib\phpseclib\Net\SSH2.php on line 3186
Call Stack
# Time Memory Function Location
1 0.2015 152184 {main}( ) ..\index.php:0
2 0.2244 2275600 handle ( ) ..\index.php:95
3 0.2246 2276880 {closure:C:\xampp\htdocs\XookCatalogFeed\handlers\import.php:157-285}( ) ..\index.php:95
4 0.2254 2303944 phpseclib\Net\SFTP->login( ) ..\import.php:170
5 5.1277 4457272 phpseclib\Net\SFTP->_send_sftp_packet( ) ..\SFTP.php:444
6 5.1278 4457592 phpseclib\Net\SSH2->_send_channel_packet( ) ..\SFTP.php:2635
7 30.0009 4457760 phpseclib\Net\SSH2->_send_binary_packet( ) ..\SSH2.php:3332
I have tried a lot of stuff but nothing seems to work. I have done some debugging (step by step) it looks like the servers accepts thje credential but then the client just loops sending empty data at _send_binary_packet.
Here is the log: https://gist.github.com/vlopez/2a48d261ab7713dbc06d
Someone, please help.
trying to get every post and comment from a facebook page, I made this function that should go through the pagination:
$req = $facebook->api("/" . $pagename . "/?fields=posts.fields(message,link,created_time,shares,likes,comments)");
function parcours_arbre($ab)
{
GLOBAL $facebook;
GLOBAL $pagename;
$next = create_request($ab['posts']['paging']['next']);
$next_req = $facebook->api($pagename.$next);
$ab_next = $next_req['data'];
$prev = create_request($ab['posts']['paging']['previous']);
$prev_req = $facebook->api($prev);
$ab_prev = $prev_req['data'];
if (empty($ab)) {
display_results($ab['posts']['data']);
} else {
parcours_arbre($ab_next);
parcours_arbre($ab_prev);
}
}
I unfortunately get the following error:
Notice: Undefined index: posts in /form.php on line 36
Notice: Undefined offset: 3 in /utils.php on line 20
Notice: Undefined offset: 4 in /utils.php on line 20
Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /sdk/src/base_facebook.php on line 1271
Any idea how i could avoid it or what is going on? Would this go away if i use the "until" statement in my api request?
Thanks a lot,
To explain each error
the variable $ab which is an argument to the function, does not have a "posts" index. You should try to var_dump this variable so you can see what it actually looks like.
same as above
same as above
the api function takes 3. The path which should just be #pagename. The method ("GET" or "POST") most likely POST because GET is causing an error. The parameters, which should be array("fields" => "posts.fields(message,link,created_time,shares,likes,comments)")
I noticed that for next you have the code
$next_req = $facebook->api($pagename.$next);
but for previous you have
$prev_req = $facebook->api($prev);
Might want to look into this.
I hope the title isn't too confusing, I'll try to explain better below.
Suppose I have a function in a separate file, functions.php:
function divide($num1, $num2) {
if ($num1 == 0 || $num2 == 0) {
trigger_error("Cannot divide by 0", E_USER_ERROR);
} else {
return ($num1 / $num2);
}
}
And another file that calls it:
include "functions.php";
echo divide(10, 0);
My error is
Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\functions.php on line 5
My question is, how do I make that error instead point to the location of the error in the main code, so I instead get:
Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\main.php on line 3
The particular reason I want this is because I have a function called load_class that simply finds a PHP file and instantiates the object inside, but if given an incorrect file name, it reports an error from inside load_class, which is technically true, but it's not particularly helpful if I don't remember where I called load_class in the first place. I would like the error to point to the file that called load_class incorrectly.
Also, I would like to write a function error() (something like below) that when given a message as a parameter would throw more "meaningful" error messages, but when done that way, the error always says it comes from error(), not from where the error actually came from!
For example, in an error.php:
/**
* error()
*
* Throws an error of a certain type
*
* #param string $type The type of error. "Fatal", "warning," or "notice"
* #param string $message A description of the error
* #return void
*/
function error($type, $message) {
switch (strtolower($type)) {
case 'fatal':
trigger_error($message, E_USER_ERROR);
break;
case 'notice':
trigger_error($message, E_USER_NOTICE);
default:
trigger_error($message, E_USER_WARNING);
break;
}
}
And in an index.php
error("fatal", "A sample warning!");
My error given is:
Fatal error: A sample warning! in
C:\Users\Derek\Desktop\projects\synthesis\sys\Error.php on line 45
But the error didn't occur in error.php, it happened in index.php! How can I make it show where it really came from?
The debug_backtrace function allows you to obtain the stacktrace as an array. You can pick the original location from there.
Next to that you need to slip into the error message to make this look-alike. Example:
function divide($num1, $num2) {
if ($num1 == 0 || $num2 == 0) {
trigger_error_original("Cannot divide by 0", E_USER_ERROR);
} else {
return ($num1 / $num2);
}
}
function trigger_error_original($message, $type) {
$trace = debug_backtrace(FALSE);
list($location) = array_slice($trace, 1, 1) + array('file' => 'unknown', 'line' => 'unknown');
$message .= sprintf(" in %s on line %d\nTriggered", $location['file'], $location['line']);
trigger_error($message, $type);
}
divide(1, 0);
The error message than shows something like:
> php test-pad.php
Fatal error: Cannot divide by 0 in test-pad.php on line 18
Triggered in test-pad.php on line 15
The downside of this is, that you need to change your code to have this "feature". If you need this for debugging your own code, it's much better that you enable backtraces in your logs. The Xdebug extension does this for you, or you can write your own error handler that takes care of that.
See as well the related question Caller function in PHP 5?. I used array_slice so that you could create an additional parameter to define the number of steps you want to go "up" in the backtrace.
Use debug_backtrace(), and debug_print_backtrace() for a full call stack. These are especially effective when using Xdebug, which will override the function to colorize the output.
I have this same problem...
#1: while 10/0 = ERROR, 0/10 = 0 is perfectly legal, you shouldn't have an exception for that.
#2: when you include a file, it effectively becomes part of this new file, so perhaps you might have to toy a little bit with things like __FILE__ and see if you can make it point it to the file before it gets included in the other file..
You can use xdebug - it will show you the stacktrace or you can register your own error handndler and display the stacktrace. Just check the example in php.net for set_error_handler().
Maybe exceptions are better to use in your case. You get the full stacktrace and can locate where the function was called without relying on some tricky code :)