Session doesn't work on MAMP - php

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.

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.

Undefined variable in IF statement (but seems correctly declared)

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!

Ajax result differs between development and production server

I have a small ajax script that gets some json data and fills some form fields when the user makes a selection.
I noticed this morning that there was an error when I ran the ajax on my development server but when run production side it worked. I am assuming this is due to some difference in error reporting between the servers but I can't figure out why.
Dev PHP version: 5.3.13
Prod PHP version: 5.3.16
I have tracked the error to some variables that were only setting if additional rows cam out of the database. I am retrieving between 1 and 3 rows.
The first row is assigned to $array1 and additional rows go into their own array as $sec_row[0] and $sec_row[1].
$array1 = ('Name'=>'George','Address'=>'52 Smith St',....etc);
$sec_row[0] = ('Alias1'=>'Jorge','Location'=>'SimCity',....etc);
echo json_encode(array('result1'=>$array1,'result2'=>$sec_row[0],'result3'=>$sec_row[1]);
Note $sec_row is only set when additional results are found.
On the live site when $sec_row is undefined the ajax returns result2 and result3 as NULL. But on the development server on my localhost it gives me an "undefined index" (if only $sec_row[0] is set) or "undefined variable" (if neither $sec_row are set) error.
I have fixed the error locally by setting $sec_row manually before encoding the json but I don't understand why I needed to do this locally but not on the production server.
Any suggestions as to what setting might cause this?
Because you probably have display_errors turned on so it will generate the undefined index error in the middle of the JSON making it invalid JSON which can not be parsed on the JS side.
Easiest thing to do other than fix the error is to turn error_reporting off. But you should fix the error, by checking if the indexes are set and then outputting the value, or null.
json_encode(array(
'result1'=>$array1,
'result2'=>isset($sec_row[0]) ? $sec_row[0] : null,
'result3'=> isset($sec_row[1]) ? $sec_row[1] : null
);
Actually, I would try to correct the error instead of simply ignoring it... Try to do something like this:
$result = array('result1' => $array1);
if (isset($sec_row[0]))
$result['result2'] = $sec_row[0];
if (isset($sec_row[1]))
$result['result3'] = $sec_row[1];
echo json_encode($result);
its to do with the error_reporting levels on your servers
so either turn them off - add the following to the top of the script, or directly in the php.ini if you have access to it - look for display_errors, set to 0
error_reporting(0);
#ini_set('display_errors', 0);
or why not build your json_encode beforehand
ie
assuming you always only have 1 $result1
for($x = 0; $x <= count($sec_row); $x++) {
$more_json .= "'result'".$x+2 ."=> ".$sec_row[$x].","
}
# strip the last comma off
$more_json = rtrim($more_json ,',');
echo json_encode(array('result1'=>$array1, $more_json);
I have not tested this, so the syntax may not be 100%, but you get the idea

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) { ...

Php error in closure compiler execution

When I run php-closure i get a PHP error
Undefined index: HTTP_IF_NONE_MATCH in <b>/php-closure.php</b> on line <b>183</b>
Line 184 of php-closure is
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
This error only happens when closure has already written the compressed javascript file to the directory once, if the directory is emptied the error does not appear. What does this error mean and how can I avoid it?
Thank You So Much!
This is most likely a negligeable message. The code should check for the presence of HTTP_IF_NONE_MATCH in the server array first, before checking its value. What is set in $_SERVER tends to change from server to server (and the headers sent by the client) so probably the original developer had that value set, and didn't think to check for it.
You could prepend the line in question with a check
if (array_key_exists("HTTP_IF_NONE_MATCH", $_SERVER))
(depending whether the program's logic and flow permit this, of course)
Or turn down the error reporting so that notices are not displayed (not recommended but mybe necessary if you can't touch the package) by putting this into the very beginning of your application:
error_reporting(E_ALL ^ E_NOTICE);
#Ivo has a great third alternative in his comment.

Categories