Undefined variable in IF statement (but seems correctly declared) - php

Something makes me crazy, I get an Undefined variable if I declare a variable in an IF statement. If I declare this variable above the IF statement, the PHP Notice disapear.
My code :
if($tab_sel==FILENAME_DEFAULT){ // FILENAME_DEFAULT = index.php
if(...){
some functions
}
$nb_pages=ceil($count/$limit) : 0;
}
echo $nb_pages; //4000 is displayed on the page (OK),
but in the Nginx log, I get :
FastCGI sent in stderr: "PHP message: PHP Notice : Undefined variable nb_pages in /.../index.php
but when I load the page, I can see that nb_page is displayed correctly (example: value is 4000), but in the log, undefined variable..
In my example, if I declare nb_pages above "if($tab_sel==FILENAME_DEFAULT){", the undefined disapear.
Any idea?

That's because you're creating the variable inside a if that can fail to pass. In this case, the variable would be undefined and PHP will throw an error since it couldn't find the variable that you are asking for afterwards.
Declare it before the if and you will be just fine.
$nb_pages;
if($tab_sel==FILENAME_DEFAULT){
if(...){

I think it might me an environment issue. Try to put both FILENAME_DEFAULT and $nb_pages into a log file:
file_put_contents(
'test.log',
date('[Y-m-d H:i:s] ')
. FILENAME_DEFAULT . "\t" . #$nb_pages . PHP_EOL,
FILE_APPEND
);
The # will prevent the warning message.

I found why it didn't work :
in case of a 404 error page (called FILENAME_404)
I also use the index.php file in which I call the variables undeclared.
that's why the Nginx error said about index.php, not because it doesn't go into if($tab_sel==FILENAME_DEFAULT){ but because in some other cases I use also index.php with tab_sel=FILENAME_404
thank you everyone!

Related

Errors: PHP Notice: Undefined index

i have this errors in error log
mod_fcgid: stderr: PHP Notice: Undefined index: url in /var/www/vhosts/mysite.it/httpdocs/wp-content/themes/colormag-pro/js/sharrre/sharrre.php on line
4 lines affected.
Are they dangerous for my site? How can i fix them exactly?
UPDATE: First 4 lines of this code in sharre.php get errors
$json['url'] = $_GET['url'];
$url = urlencode($_GET['url']);
$type = urlencode($_GET['type']);
if(filter_var($_GET['url'], FILTER_VALIDATE_URL)){
if($type == 'googlePlus'){ //source http://www.helmutgranda.com/2011/11/01/get-a-url-google-count-via-php/
$contents = parse('https://plusone.google.com/u/0/_/+1/fastbutton?url=' . $url . '&count=true');
Not too bad, annoying. A 'Notice' is something that PHP can carry on with but may cause something else to break, but on the same hand might not cause anything to break at all. A lot of the time 'Notices' can be ignored and are generally turned off in production environments.
Personally I hate to see 'Notices' in my code but that's just me.
Either $json['url'], $_GET['url'], or both are not set so check both and see which one is not being set, from there you will be able to trace back to what is supposed to be setting the variable and fix it.
Use an isset() to check if each one is set before the first line of the code you pasted to check if either one is not actually set.
This is not dangerous for your site per se. The variable(s) in the GET are not set (variables $_GET['url'] is not set). If you plan to use it later in your PHP script, you should set them before, on the previous page. So, there are two pages, first one and your PHP page - the second one.

Session doesn't work on MAMP

When i try to run my site on localhost i get an error:
Undefined index: log in ... on line 137
Within this file there is a line:
if (!$_SESSION['log']) { ...
Everything works on server, but not on localhost. What can I do to fix it?
There is probably a difference between the level of error reporting between the server and your local setup.
If you want to check if the variable is set (assuming that a session has been started...), you should use:
if (!isset($_SESSION['log'])) {
Or if you want to check if it is not set and / or empty or false:
if (empty($_SESSION['log'])) {
Both will not generate any warnings for non-set variables or array indices.
It probably isn't working "on server," but is instead just not showing the error message to the page.
You can fix the warning re: the index by changing your if statement to this:
if (isset($_SESSION['log']) && !$_SESSION['log']) {
Or to whatever condition you need it to be.

How do I define fire_event, undefined fire_event error

I'm cleaning up all the errors in an old script and I'm getting one that says undefined function fire_event. How do I define fire_event. everything works fine but i've turned on error_reporting(E_ALL); to fix all hidden errors
Fatal error: Call to undefined function fire_event() in /home/social/public_html/includes/dologin.php on line 96
if (strpos($en['redirect'],'index.php?req=login')) unset($en['redirect']);
$redirect = (isset($en['redirect']) && $en['redirect']!='' ? $en['redirect'] : constant('dir').'members.html');
fire_event('member_login',$line['m_id']);
header('Location: '.$redirect);
exit;
}
That has to be some custom function and we don't know what belongs in there. Search all source files, because you most likely removed or changed a dependency and/or include() or required() line.

Undefined index: When converting cookie value to variable

The problem
The following code produces this error from the line "print $readerStatus" -
Undefined index: readerStatus
Code
<?php
//Get Cookie Value
if (isset($_COOKIE['readerStatus'])) {
$readerStatus=$_COOKIE['readerStatus'];
} Else {
$readerStatus="Not Set";}
echo "The value of Cookie readerStatus is " . $_COOKIE['readerStatus'];
print $readerStatus;
?>
Background
The goal is simply that if a cookie is set I want to pass the value into a Javascript. My strategy is as follows:
Get the value from the cookie
Set a variable to the value of the cookie
Then use a php echo inside of the Javascript to transfer the value.
It works as expected but Eclipse is giving me the error and so I assume there is something wrong with the above code.
I'd appreciate any pointers on possible sources of the problem.
Thanks
Is this working?
<?php
//Get Cookie Value
if (isset($_COOKIE['readerStatus'])) {
$readerStatus=$_COOKIE['readerStatus'];
} else {
$readerStatus="Not Set";
}
echo ("The value of Cookie readerStatus is ".$readerStatus);
print ($readerStatus);
?>
This is a warning, not an error. However, you can skip the error by using array_key_exists. Generally, I'm not a fan of isset for this kind of checking.
if (array_key_exists('readerStatus', $_COOKIE))
{
$readerStatus=$_COOKIE['readerStatus'];
}
else
{
$readerStatus='Not Set';
}
echo 'The value of Cookie readerStatus is '. $readerStatus;
Some IDEs are less forgiving than the PHP parser itself. That being said, do you get any errors or notices when running the code? Variables in PHP are implicitly declared, so the undefined index message is simply a NOTICE (that can be ignored) regarding the accessing of an array element without it existing first.
If you check it exists prior to accessing it like this, you shouldn't have a problem.
$readerStatus = isset($_COOIKE['readerStatus']) ? $_COOIKE['readerStatus'] : '';

What does #$_GET mean?

I don't understand why someone is using the # in the code, I have seen it with mysql connections but I don't know what it means.. thanks!
$player_name_orig = #$_GET['player'];
if (!$player_name_orig) {
die('You must specify a player name');
}
The # is the error suppression operator.
In this specific context, it's a (wrong!) way to avoid PHP giving a notice if the player key does not exist in $_GET:
If you try this:
unset($_GET['player']); // to make sure
echo $_GET['player'];
You get:
Notice: Undefined index: player in F:\dev\www\index.php on line 35
While if you try this:
unset($_GET['player']); // to make sure
echo #$_GET['player'];
There is no output.
The correct way to do this:
if (empty($_GET['player']) {
die('You must specify a player name');
}
The # will stop any errors from appearing and return false on an error.
So in your code if $_GET['player'] does not exist then the code will go into the if statement
# Means to ignore errors such as the variable not being set.
the "#" is used to prevent any warning or error message to appear. It's a really bad habit. Doing that a lot of hidden operations are done (removing error handler, and putting it back after).
The right way to do that operation is:
// my_security_filter() is a function that can render FALSE and remove any dangerous thing
$player_name_orig = array_key_exists('player',$_GET)? my_security_filter($_GET['player']) : FALSE;
if (FALSE===$player_name_orig) { ...

Categories