PHP Undefined variable in Apache error_log - php

I'm getting a series of:
"Undefined variable: loginError in /Library/WebServer/Documents/clients . . ."
entries in my Apache error_log which I would like to prevent. I have a simple login.php page which, if there's an error logging in sets the $loginError variable as such:
$loginError = '<p class="text-error">Login Error: '. $layouts->getMessage(). ' (' . $layouts->code . ')</p>';
If there's no error logging in it does this:
$loginError = '';
I then output any errors as such:
if ($loginError !== '') { //line 112
echo $loginError; /line 113
}
I'm getting the entries for the line 112 and 113 noted in my comments above. Anyone tell me how I can prevent the entries appearing? I'm using PHP Version 5.3.6.
thanks

Its saying you should check it is set before using:
One way is with isset()
if (isset($loginError) && $loginError !== '') {
echo $loginError;
}
But in your particular case you may as well use !empty()
if (!empty($loginError)) {
echo $loginError;
}

Hard to say without seeing the rest of your code. Trace through your logic to make sure that every possible branch initializes loginError at some point in its execution. Even better, set it to a default value before you go through the logic.

Related

Undefined index: HTTP_USER_AGENT

I am trying to perform an action when a specific user agent visits a link.
So i have my code like this:
//if browser is not Mozilla/4.2, then do something.
//but if its Mozilla/4.2, do another thing.
if(strlen(strstr($_SERVER['HTTP_USER_AGENT'],"Mozilla/4.2")) <= 0 ){
// Do something
} else {
//Else do another thing code follows.
}
The above code is working but it keeps giving this warning in the error log "Undefined index: HTTP_USER_AGENT"
The solution i saw used pregmatch, but am targeting only a single user agent.
Any help will be appreciated.
You simply need to check the existence of the index (HTTP_USER_AGENT) on $_SERVER and if it's not set then set it to empty string.
This can be achieved by doing;
$userAgent = ! empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if(strlen(strstr($userAgent,"Mozilla/4.2")) <= 0 ) {
// Do something
} else {
// Do something else.
}

How do I fix a undefined index error in Drupal 7?

Problem
I am getting a "Undefined index: node in include()" notice in Drupal for the below line of code. I've tried the below solution but I am still receiving the error. Any ideas?
Code
$url = drupal_lookup_path('alias', 'node/' . $related['node']->nid);
The solution I tried
isset($related['node']->nid) ? $related['node']->nid : "";
Question
Does anyone know why this error continues to occur?
The error suggests the $related array variable doesn't have a node index.
First, make sure you're retrieving the node object correctly.
Then, perhaps try it like this to avoid errors:
<?php
$url = '';
if (isset($related['node']) && is_object($related['node'])) {
$nid = $related['node']->nid;
$url = drupal_lookup_path('alias', "node/$nid");
}

CakePHP Notice (8) raised : Use of undefined constant inList - assumed 'inList'

Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.

$write_result->errors throws "Undefined Property" when there are no errors

Using pre-written code from Bronto, it builds a soap client, calls a function on it, then parses the results. The parsing code looks like this:
if ($write_result->errors) {
print "There was a problem adding or updating the contact:\n";
print_r($write_result->results);
exit;
} elseif ($write_result->results[0]->isNew == true) {
print "The contact has been added. Id: " . $write_result->results[0]->id . "\n";
} else {
print "The contact's information has been updated. Id: " . $write_result->results[0]->id . "\n";
}
Whenever there ARE errors, they get caught and printed by the first if statement. But when there AREN'T errors, the console gets an "Notice: Undefined property: stdClass::$errors" message printed out. Is this right? Is there a way to turn off the notice? It doesn't cause any problems, but I can see how it would confuse a non-techy reading the output logs.
Check if the property exists instead of accessing it directly:
if (isset($write_result->errors))
Or to check if it exists and not empty at once (just to be sure in case the API changes and provides an actual empty array or empty string if no errors occured):
if (!empty($write_result->errors))
You could check first if the property exists:
if (property_exists($write_result, 'errors'))
Check that the property exists first:
if (property_exists($write_result, 'errors') && $write_result->errors)
{
// ...
}
See: property_exists.

Can't use function return value in write context, PHP can't figure out why

This is my code :
PHP:
if(isset($_COOKIE("cookie_roof_angle")) && isset($_COOKIE("cookie_roof_direction")))
{
$roof_angle = intval($_COOKIE("cookie_roof_angle"));
$roof_direction = $_COOKIE("cookie_roof_direction");
$solarsell_page05_rendement = mysql_query("SELECT value FROM solarsell_page05_pvgis WHERE angle = $roof_angle AND azimut = " . $roof_direction. " ");
echo $solarsell_page05_rendement;
}
else
{
echo "no values";
}
I'm getting this error message :
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\Development\phpFunctions.php on line 19
After some searching around on the web & stackoverflow.com, I found out it may be caused by the isset function, could anyone please explain if this is the problem and why?
If this is not the problem, maybe I did something wrong in my code part, but I can't figure out why.
The Cookies where both set when I got the error.
Sincerly,
Harmen Brinkman.
Syntax error:
$_COOKIE["cookie_roof_angle"] instead of $_COOKIE("cookie_roof_angle")

Categories