Wednesday, 2 January 2013

Firefox disappointment

What has happened to Firefox?

It always used to be a reliable, honest, hassle-free browser. But when I found javascript from a shower called Superfish injected into my pages, I changed my view.

There seems to be no ready way for a user to find out which Add-On or Extension is responsible for interfering with pages.

Trial and error suggested that for me it was the FoxIt PDF reader that was responsible (but I am still not 100% sure). But why should one have to use trial and error. Where is the Firefox feature that quickly identifies running addons and extensions, and shows what each is doing to your browser?

Friday, 15 June 2012

Building an SQL query in PHP: beware formulae

Got in trouble with this

$sql.=" SET commsq.attempts = $newNumberOfAttempts ";
$sql.=", commssitebasics.uNextContact =".$thisU+DEFINED_CONSTANT;
$sql.=", commssitebasics.somethingElse=2";
which produced a very odd result


SET commsq.attempts = 1 1, commssitebasics.somethingElse=2


what was happening? Answer: the first line is fine, but in the second line, PHP doesn't like the "+" and ignores everything before it, adding just the value of DEFINED_CONSTANT to the string. 


Answer - always put brackets around any appended value which is the result of a computation:


$sql.=", commssitebasics.uNextContact =".($thisU+DEFINED_CONSTANT);


Thursday, 17 November 2011

jpgraph for php: The image xxxx cannot be displayed, because it contains errors ...

... or perhaps you have some blank space (perhaps a blank line??) before the opening PHP tag

Wednesday, 9 March 2011

ezPDF - adding a horizontal rule

The ezPdf class for PHP is fairly basic, but none the worse for that. I found I couldn't get a Horizontal Rule to go below the text I'd just output, but it was very easy to write a function that would do it:

function ezHR($lineThickness){
//writes horizontal rule of given thickness, respecting left and right margins
$this->setLineStyle($lineThickness);
$y=$this->ezGetDy()-10;
$r=$this->ez['pageWidth']-$this->ez['rightMargin'];
$this->line($this->ez['leftMargin'],$y,$r,$y);
}


Add this to class.ezpdf.php and then call it with 
$pdf->ezHR(2);

Friday, 4 March 2011

Troubleshooting php.exe when run from Task Scheduler

I looked high and low for ideas on how to keep open the output that flashes up when you run a php page using task manager (unless you write a bit of vbs to hide it entirely).

eg, when Task Scheduler runs:

C:\wamp\bin\php\php5.3.4\php.exe d:\test.php

how can you check to see whether there are errors in test.php? Obviously you could open as any other php page but what if the errors are only applicable to the page being called from Task Scheduler?

The nearest I have got is to open cmd.exe, and then paste in the command-line string.

Wednesday, 23 February 2011

php "failed to open stream - no error"

Chased this error (when calling php function file_get_contents) for ages, and eventually spotted: a mal-formed URL: one of the slashes missing after "http:"

Why did it take so long? Because as part of my debugging I echoed the url that I was calling, then copied and pasted that into my browser (Firefox 3.6.13). Result - the page loaded fine.

Good old Firefox had corrected the error without comment.

Moral: sometimes fixing things quietly isn't as kindly an act as one might imagine.

Sunday, 6 February 2011

PHP "Deprecated" error messages

PHP 5.3.x throws out plenty of "Deprecated" messages. Having set
error_reporting = E_ALL & ~E_DEPRECATED
and confirmed in phpinfo() that I was getting the expected value of 22527, I was still getting the wretched messages.

The cause?

This code, which an "include" called in my own coding as I launched each page:
error_reporting  (E_ALL);

doh!

(If you've found this, perhaps that is your problem, too?)

Thursday, 3 February 2011

Ha7net - adding a user password

The Ha7net (1-wire controller accessed via TCP/IP) offers the option to set a user password. The problem is that the documentation doesn't bother to tell you how to send the username/password.

When reading HTML from the Ha7net via a browser, I found that Firefox was happy if I just appended "&user=user&password=pword" (you can change "pword" but not "user"). But IE8 still threw up a dialogue box asking for the two entries. And in php file_get_contents failed with a 401 error.

The solution is that the username/password need to be submitted in encoded form. Here is an example in php:

$context = stream_context_create(array(
        'http' => array(
            'header'  => "Authorization: Basic " . base64_encode(USER_NAME.":".USER_PASSWORD)
        )
    ));
    return file_get_contents($url, false, $context);

I have suggested to eds that the manual needs some content on use of passwords. For now, I hope this might just save someone else a bit of time.

Tuesday, 9 November 2010

HA7net+iButton: "Internal DS2417 is not responding" and "Short Detected on 1-wire Bus"

Got these messages when I placed an iButton (DS1990R) on a simple magnetic  probe (ML9092).

The cause? Ground and Data reversed on the probe - Ground at the edge, data in the centre.

Sunday, 31 October 2010

AJAX slow? Check DNS Lookup time

Struggling with slow response times on AJAX updates, I stumbled on 1-sec+ DNS Lookup times for each operation.Why? Turned out this was a Firefox-only problem, with sparkling performance in Chrome. So why Firefox? Turns out that the default setting is for Firefox to try an IPv6 lookup first, and then drop back to IPv4. Turning that off (good note here) made the world of difference.

Thursday, 21 October 2010

jpGraph - cannot change line colour (or color)

Using jpGraph, an excellent package for plotting graphs from database data via PHP, I hit a problem: setting
$lineplot->Setcolor("blue"); 
had no result. Nor did
$lineplot->SetWeight(2);
Eventually, I sent off an enquiry to jpGraph - and back came a very prompt reply (from the Far East):


in version 3.5.0b1, methods to change designs of each plot should be used after $graph->add($plot) method.
This is really counter-intuitive. But it works.

So it goes like this

$bplot = new BarPlot($data1y);
$graph->Add($bplot);
 
// NOW change properties of the plot - these work ONLY after calling Add()
$bplot->SetWeight(0);
$bplot->SetFillGradient('#FFAAAA:0.7', '#FFAAAA:1.2', GRAD_VER);    
 


There is a note in the documentation included as part of the package, at:


jpgraph-3.5.0b1/docs/chunkhtml/ch29s02.html

Tuesday, 19 October 2010

HA7net - writing invalid HTML

The HA7net is a neat unit - a "1-wire Ethernet Bus Master" that purports to send readings from the 1-wire devices (eg temperature sensors) in an easy-to-parse HTML table.

But with the current firmware of the day (1.0.0.22) it sends INVALID HTML (as reported by the w3c checker). I am not an expert in parsing (domDocument in php is clever but not brilliantly documented for the newcomer) and it took me a couple of hours to trace this problem. Thanks HA7 net!

The problem is that the HA7net adds a hidden element like this:



but it neglects to wrap this in "TD" tags - it sits after the last closing /TD but before the /TR on each line

For now, I have written a bit of PHP to correct it:


function mendEDSbrokenHTM($content){
    $output;
    $delimInput="
    $delimCloseTR="";
    //add
tags around each Device_Exception_Code input
    $tA=explode($delimInput,$content); //will produce n+1 elements
    $output=$tA[0];
    for ($b=1; $b
        $tB=explode($delimCloseTR,$tA[$b],2);
        $output.="".$delimInput.$tB[0]."".$delimCloseTR.$tB[1];
    }
    return $output;
}


(this isn't perfect: there will be more cells in each data row than in the header row. But it worked well enough for me).

But why should I have to muck around like this? - Easy-to-parse HTML is a key selling point on this supposedly mature system.

Not a good first encounter with the HA7net.

Windows Task Scheduler - running more often than every 5 minutes

The problem - I wanted to interrogate a network of temperature sensors every two minutes. Not too difficult to do it once, and to package that into a bat file. But how could I run the bat file every two minutes when Windows Task Scheduler offers nothing more frequent than "every 5 minutes"?

The answer - stop respecting authority (in the shape of the options provided) and get assertive. If you want to run the program every 1 minute,  type in "1 minute" into the selection box. 

And it works! at least for me (Windows 7 Pro 64-bit).

* * *

NB - to make the BAT file run invisibly without having to install Visual Basic:

Save the following line as invisible.vbs (use Notepad Save As... All Files)

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Confirm you have Scripting installed (By typing wscript in the Start Run dialog) 

Use the following as the script to call from Task Scheduler (Replace the "MySubFolder" with your file paths)

wscript "C:\MySubfolder\invisible.vbs" "C:\MySubfolder\MyBatchFile.bat"

Thanks to kimsland at http://forums.techguy.org

Wednesday, 6 October 2010

Configuring MySQL ODBC connection

Along with obvious things like username and password, the form demands a Data Source Name. The documentation isn't explicit, and I spent a while worrying what this should be.

In fact it can be anything you like: it is just a make-it-up "name for this data source" rather than a work-out-what-it-should-be "Data Source Name"

Wednesday, 11 August 2010

Review - mySQL Ajax Table Editor

This is much more like what I need to create a user-friendly data entry/editing system for lay users.

Documentation is on the terse side (I could have used some examples) but it does look like I will be able to get it to do odd bits of post-entry/edit data manipulation.

On user-friendliness, it scores modestly well. It is very easy to specify a user-friendly title for a data item.

BUT - it doesn't cope at all with data that includes an apostrophe.

PHPmyEdit: doesn't work with WAMP?

Looks like a very comprehensive offering, but ...

Installed it under WAMP and got:

Notice: Undefined index: db in C:\webserver\Billing\phpMyEdit\phpMyEditSetup.php on line 65

Notice: Undefined index: tb in C:\webserver\Billing\phpMyEdit\phpMyEditSetup.php on line 66

Notice: Undefined variable: db in C:\webserver\Billing\phpMyEdit\phpMyEditSetup.php on line 189

Notice: Undefined variable: tb in C:\webserver\Billing\phpMyEdit\phpMyEditSetup.php on line 192

Searching suggests that others have hit this with WAMP. There also seem to be some current issues (Aug 2010) with compatability with PHP 5.

The search goes on

PHPmySQL - review of demo




Simple enough to use, but documentation is very poor: a description of each screen in the sequence. At a first look, it appeared as only the connection data can be saved: if you want to modify an existing set of pages, you have to specify your requirements from scratch again.

When you are finished, the "Generate" action copies a bunch of files to your chosen folder. Only "database.php" and "config.php" have the current date - all the others are much older, which confused me: I eventually twigged that I needed to open "index.php" to see the resulting application.

The result is competent enough, I am sure (demo does not allow use of foreign-key lookup), but very much the IT-person's data entry experience: not suitable for lay users, in my view: there didn't seem to be any option to give data fields user-friendly display names, for example.

Monday, 9 August 2010

Working with Multiple Databases under MySQL, PHP and phpMyAdmin

I couldn't find clear and concise guidance, so here is my offering (after much miserable experimentation):

Establish a Connection, eg

$glob_dbConnection = mysql_connect($db_host, $db_user , $db_pass) or die ('D5518 Error connecting to mysql');


Unless you are doing anything fancy, you don't actually need to refer to $glob_dbConnection again, as this becomes the default.

This connection then allows access to as many databases as $db_user has privileges for, so you can specify queries like this:

$sql="SELECT databaseCommon.products.*, databaseCustomer.purchases.* from databaseCommon.products INNER JOIN databaseCustomer.purchases ON ...

But setting the Privileges is a bit of a pain, and (to me) far from obvious.

In phpMyAmin, click on the link to "Server: localhost" to get at all the top-level settings.

Clicking on the Privileges link will take you to the User Overview. This allows you to Add an extra user, and lists existing users, and tells what Global Privileges each has, but it says nothing about which databases the user can access.  To fix that, click on Edit Privileges, and find the second section, which is marked "Database-specific privileges". There is a select box marked "Add privileges on the following database" - with a default of "Use text field".
Using the default, you can type in the database name, but it is safer to open the Select Box and choose the database name from there.

Having done this, press Go. The page reloads and you can now specify which actions are allowed for this user for this database (eg Select+Insert+Update but not Delete, perhaps). Tick the boxes and press Go. You may well get an error message, eg
#1141 - There is no such grant defined for user 'billinguser' on host 'localhost'
which appears to be spurious. Click on the link (eg 'billinguser'@'localhost') and it will now offer you the chance to edit Global privileges at the top of the page; further down, it lists the Database-specific privileges and you will see the specific permissions that you have added.



Good Luck

Sunday, 8 August 2010

Chrome may fire OnKeyUp event when you give focus to a SELECT box

Trying to create an select-as-you-type text box using Javascript, I wanted to open a SELECT box listing matches for text typed so far: no problem.

The challenge came when I wanted use of down-arrow from the text box to result in the first item in the select box being displayed and selected. In Chrome, setting the focus to the SELECT box was fine for an instant, but also resulted in the "onkeyup" event being fired and the focus moving away from the SELECT box. That isn't correct: onkeyup should only fire if the user has clicked on the SELECT box, and I had moved there programatically.

The solution was to use the onkeyup event to call a function that would specify thisObj.selectedIndex=0; - this brought focus back to the SELECT box. Of course, that then needed a boolean value setting in the onKeyDown so that Icould distinguish between normal and "phantom" keyup events.


... select id='monikerSelect' onkeyup='handleSelectKeyUp();'  ...


function handleSelectKeyUp(){
//CHROME triggers this immediately after passing focus.
    //We cannot stop it firing, so we need to try and make it put right what it mucks up

if (glob_selectDownHasBeenRun){
    //this is a keyup which follows a keydown: normal use
} else {
    //this is an orphan keyup that Chrome has invented for itself: THE PROBLEM CASE
    document.getElementById("monikerSelect").selectedIndex=0;
}
glob_selectDownHasBeenRun=false;
}

Wednesday, 7 July 2010

PHP and buttons

PHP correctly reports the value of a "type=submit" button in the $_POST array. But if you use a type=button button (eg because you want to validate data before deciding whether to submit) then the value of the button pressed is NOT present in the $_POST array - the button name isn't in the array at all.

Not a big deal (easy enough to set a hidden value, assuming that you use Javascript) but that didn't stop me wasting a lot of time trying to work out what I was doing wrong.