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
Related
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.
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.
I'm writing a web application in PHP, and I'm getting the following warning in Google Chrome only:
Warning: Cannot use a scalar value as an array in
M:\xampp\htdocs\MyProjectTitle\php\classes.php on line 18
Background: Please note that the web app works just fine in all browsers I tested (Firefox, Chrome, IE), but the warning appearing in Chrome is bugging the hell outta me.
Notes: I have tried restarting Chrome and clearing the Chrome cache. I have tried restarting the computer. I have tried searching the source code in FF and IE to see if the warning was just hidden, but to no avail. The warning is only popping up in Google Chrome, and I have no idea why.
UPDATE: Even though Safari also uses WebKit, it does not show the PHP warning. For some reason, Chrome is the only one that has the PHP warning. Weird stuff!
UPDATE: Here's the first 21 lines of code:
class Presenter {
public function includeFile($filePath) {
if (is_readable($filePath)) {
include $filePath;
} else {
echo '<p><strong>ERROR 404</strong></p>';
echo '<p>The resource you requested could not be located.</p>';
}
}
public function sanitize($string) {
return preg_replace("/\s/","_",$string);
}
public function set($varname,$value) {
$this->$varname = $value;
$_SESSION[$this->name][$varname] = $value;
return $this; // enables chaining!
}
// And there's more code after this, but I have cut it out here.
}
Note: The above code is inside a PHP class.
UPDATE: I know exactly what lines of code are causing the warning in Chrome (although I still have no clue why Chrome is the only browser that shows the warning). Since my webapp works perfectly despite the warning in Chrome, I have used # to suppress the warnings in Chrome (I'll fix the warning later).
Most likely it's this:
$_SESSION[$this->name][$varname] = $value;
if $_SESSION[$this->name] was not defined as an array earlier, you'll get the "can't use scalar as array" error. e.g. if you do
$_SESSION[$this->name] = 42;
$_SESSION[$this->name][$varname] = 'will not work';
the error is triggered.
I have the feeling that Chrome sends different data to the server, therefore you are having this error. If there's any $_POST data, or $_GET data please verify that with what other browsers send. You can easily display the contents using print_r() or var_dump() whichever you choose.
This usually happens because you do something browser-specific(maybe you do something IE specific, and firerfox-specific, but forgot to add a "default" case for other browsers?)
UPDATE: I know exactly what lines of code are causing the warning in Chrome (although I still have no clue why Chrome is the only browser that shows the warning...If you know, please let me know!). Since my webapp works perfectly despite the warning in Chrome, I have used # to suppress the warning as a temporary fix until I can fix it for real later.
Basically that is happening because you are trying to access a UNDEFINED variable (or not set variable).
PHP omits MANY common programming mistakes, that other languajes don't permit.
Ok if you start a program like:
<?php if(eregi("hi",$hello)) { echo "hi"; } ?>
Will get this "message"
Notice: Undefined variable: hello in C:\var\www\test.php on line 1
you sould predefined it as:
<?php $hello = "hello"; if(eregi("hi",$hello)) { echo "hi"; } ?>
Or a conditioned to avoid elimination of previous values:
$hello = isset($hello) ? $hello : null;
This code will send you a deprecated function message also.
any why this messages are caused by the PHP.INI configuration on your server, this is happening because you had something like this:
display_error = on;
error_reporting = E_ALL;
And sould be something like:
display_error = on;
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
This will remove all NOTICES messages and make PHP more permisive, also you can add ~E_DEPRECATED if you get deprecated messages,
how ever getting this messages, means that you need to be a more secitive programmer! Yes is more tediuos but some one has to make it!
Probably because you need to do ctr+f5 or restart in your other browser. It probably has a previous version of your page cached, happened to me a lot with Internet Explorer 8.
I am trying to enumerate through a perfectly valid array using php 5.3.5 on Joomla 1.5. Whenever I try to access the array I get the white screen of death. If I add a die() statement right after then I get the array, (but of course, execution after that is halted). I purposely put no code after array call and die() for debugging purposes. Removing die doesn't echo the array. Has anyone else had this issue before?
Edit: yes, turned error checking on. WSOD is BLANK.
**in the View class:**
$seminarsRefDB =& JFactory::getDBO();
$seminarsRefQuery = [MYSQL STUFF]
$seminarsRefDB->setQuery($seminarsRefQuery);
$seminarsRefList = $seminarsRefDB->loadAssocList();
for($i=0; $i<count($seminarsRefList); $i++) {
$classAppendix = $i;
$seminarselects[] = JHTML::_('select.genericList', $seminar_options, 'seminar_title[]', 'class="seminardropdown" style="width:200px;"', 'value', 'text', $seminarsRefList[$i]['value'], 'seminar'.$classAppendix);
};
$this->assignRef('seminarsArray', $seminarselects);
**In the Default Template**
print_r($this->seminarsArray[0]);
die;
END
I have another array called speakersArray which is echoed perfectly. I copied this code verbatim from the backend of my site where both arrays show no problems.
Used get_included_files and the default template is the last file included, so execution stops there.
You should turn on display_errors and error_reporting to E_ALL so you don't get a white screen of death and have your server tell you what errors it is getting.
It sounds to me that if its a big array and your passing it around, you could be running out of memory at some point in the code. By placing a die right after the array, you may have not hit that threshold yet.
Though iLLin's approach is fine for development testing, this is bad practice for a live site. Assuming you have access to your server, view the error log file to find out what is going on here.
tail -f error_log
I'm trying this:
function send_sms() {
$liveQuery = $this->db->get('liveList');
$counter = 0;
foreach($liveQuery->result() as $row):
$counter = $counter+1;
echo("Not hatin', just iteratin'. Message " . $counter);
endforeach;
}
When liveList has 8000 records it runs just fine, but when I try with 9000 rows it generates a download of a blank, 0 KB, document. Anyone know why this happens?
This post should help you:
http://codeigniter.com/forums/viewthread/129194/
I am not familiar with codeigniter, but do you have php error reporting on? There could be an error being thrown that's hidden.
Blank pages are normally a symptom of time-outs or crashed scripts. You might be able to find the exact reason in the server logs. Make sure you have the log_errors directive enabled in your PHP installation. Also, have a look at the web server logs.