Realex payments error 505 “You cannot use this service from there”

September 2, 2008

Did a booking engine for a hotel lately, and I wanted to record the booking details before redirecting the user to the payment processor’s site (realex in this case), so the final form was submitted to my own application, then I used php’s curl functions to get the payment page from realex.

In the response from realex’s server to the http request sent by curl I was getting an error with error code 505 and the message “You cannot use this service from there”. Before payment goes live for an account, realex asks for the url that you’ll be redirecting to their system from (the “incoming url”, they call it) and then they reject requests from any other “incoming url”. What this means, it turns out, is they check the “Referer” header in your incoming http request and make sure it matches the “incoming url” you’ve supplied to them. The request I was sending via curl wasn’t sending a “Referer” header, hence the error. Solved the problem like this:

$ch = curl_init(PAYMENT_PAGE_URL);
curl_setopt(...);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Referer: <myurl>"));
curl_exec($ch);
curl_close($ch);

Fast server-side rejection of large image uploads using $_FILES

August 28, 2008

Discovered today you can report to a user if the file(s) he/she is uploading is too large without having to wait for the file to finish uploading by checking $_FILES - the $_FILES array for each form input of type “file” has an element called “error” which returns an error code without actually uploading the file if the file is larger than than upload_max_filesize in php.ini or $_POST["MAX_FILE_SIZE"]. It can do this because a “Content-length” http header is sent to the server first, and the file itself is then sent in the body of the http request.

Here’s a very simple example.

The form:

<form enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
    <input type="file" name="image" />
    <input type="submit">
</form>

The php:

if ($_FILES["image"]["error"] == UPLOAD_ERR_FORM_SIZE)
{
    echo "file too big!";
}

Note that you shouldn’t just use MAX_FILE_SIZE as I’ve done above, you also need to set upload_max_filesize appropriately in php.ini


setAttribute not working for onclick in IE

August 8, 2008

You can’t use javascript setAttribute() to set an onclick event on a link in Internet Explorer, I’ve discovered. You have to use <element>.onclick = <something> instead.

See  here http://justinfrench.com/index.php?id=25 and here http://www.quirksmode.org/js/events_tradmod.html


Changing style.display using javascript not working in IE

August 8, 2008

I was using a fairly simple js script to show/hide a div. Worked fine in firefox, didn’t work at all in IE (and had the unexpected side-effect of hiding the link I wanted to click on to toggle the show/hide). The problem code is below:

<a href="#specialConditions" name="specialConditions"
onclick="toggleDisplay('specialConditions');"> Special conds</a>

<div id="specialConditions" style="display: none;">
    ...
</div>

The function toggleDisplay() just sets style.display=’none’ if it’s equal to ‘block’ and vice-versa.

The problem turned out to be the ‘name=”specialConditions”‘ in the link - IE seems to confuse names and IDs in getElementById(), so document.getElementById(’specialConditions’) was returning the link instead of the div.

So I changed the value of “name” in the link and it worked fine:

<a href="#specialConditionsAnchor" name="specialConditionsAnchor"
onclick="toggleDisplay('specialConditions');"> Special conds</a>

<div id="specialConditions" style="display: none;">
    ...
</div>

Lazy loading of object variables in php using __get()

August 1, 2008

Was doing a(nother) shopping application lately. In the application each Product (a class) can have many images, but they’re not displayed in the search results page or the basket page, so it seemed a bit daft to be reading the database and loading them into memory in the constructor.

What I did instead was to use the magic method __get() to load the images into the object when they were needed. __get() is called whenever something tries to access a variable that is not set or publically accessible, so basically I used that to load the images whenever some other piece of code tried to access Product::images.

Here’s the actual code:

public function __get($var)
{
    eval("\$this->_load".$var."();");
    return $this->{$var};
}

So if some other piece of code made a Product object called $product and then tried to access $product->images and the images haven’t already been loaded, a (private) function called $product->_loadimages() is called. If the images HAVE already been loaded then __get() is not called.

Very handy.

One other thing I had to so was to unset() Product::images in the constructor, so that __get() would be called when something tried to access it. I could have avoided this by making $images private, but it behaves as a public property so I didn’t want to.


javascript not working in Firefox

July 25, 2008

Working on a page with lots of ajax and javascript and it just stopped working. Nothing worked, not even alerts. Eventually I went to close firefox and open it again, and when I did it told me there was a firefox process still running and I had to close it before starting FF again, so I went into the task manager and did and it worked fine from then on.

What happened was I had had an infinite loop in my javascript code while working on it, and it seems that kept running away in the background and stopping ff running any more javascript.


Outlook Express images in IE cache

July 8, 2008

Was testing a mailout today, and checked it in Outlook Express, which was showing an image from a test I sent yesterday. In the end I had to clear out the Internet Explorer cache to get the image to update. Very odd behaviour.


Strange behaviour of $_POST in ModX snippets

May 19, 2008

I spent half the morning trying to figure out why I was getting bizarre values for $_POST variables in a ModX snippet and the answer is … caching! Should have thought of it earlier :( In ModX all php snippets are cached, if you want them not to be you’ve to call them using different tags. See http://modxcms.com/document-caching.html


Web application security

April 28, 2008

I’ve been keeping an eye on The Hacker Webzine for a while now just so I’m up to speed on security issues, and today Roland posted a very handy (I hope) .htaccess file that will act as a firewall against 99% of known attacks against web applications. Check out his “My web application firewall” post here.


Zend_Form

April 22, 2008

I was kinda mystified as to why anyone would make forms using php rather than just doing them with html, but the PEAR forms module seems to be popular and before Zend put a forms module in their framework there was a lot of moaning about its absence, so I thought I’d try out the ZF module and find out what the fuss is about.

Still mystified. Using Zend_Form wasn’t any quicker from a coding point of view than just making the a form in a view and having a separate validation object, and it’s way slower to run. Won’t be rushing to use it again.



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser