Paul's Dev Blog – iGonzo.net

Code not fit for human consumption.

Archive for the ‘Programming’ Category

IIS and multiple IPs

without comments

So it was that we had a site that was needing to pull in some XML data from a clients internal system. Not a huge deal, we do it all the time. The client needed to restrict the data to an IP address for security. Also not a problem, we do that quite regularly. But as it turned out, it was a problem. A small, annoying problem that the client caught onto.

You see, in an IIS server that has multiple IPs going to it, you can assign an IP to a site. This is what we did here; specifically set the site to respond to a certain IP. The client allowed an exception to that IP. But it still would pull in data.

Begin frustration.

After more than a day of fiddling around with setting, I asked the client to pull up the IP of the site we are working on. They did, and confirmed that the site was from that IP. But there was also something else. Another IP entry in their firewall logs. Turns out, that was one of our IPs.

Weird. So I asked the client to add exceptions to all the IPs running to our server. He did. And low and behold, the site is pulling in the data now.

Begin confusion.

The data grab is being done on the backend through an XML load. Apparently, the server decided to use whatever available channel to grab the data. In this instance, it was the first IP on the list. So it turns out that the IP we specifically set the site to respond to is just that, it responds to just that IP, and responds only. Getting data, however, is a different matter it seems, ad the site will use whatever available IP, regardless of what the site is set to respond to.

End confusion. End frustration.

I havent seen where to set IIS to only pipe data trough the IP we assign it, but at this point I am not really concerned about that. It works and for now, I will take that as a win.

So in the future, if you use IIS, be sure to allow exceptions for all IPs to the box when needing to get data from an external source. Save yourself the frustration. Then go have a beer.

Mmmm, beer. :)

Written by iGonzo

July 19th, 2011 at 12:42 pm

Javascript hates spaces

without comments

It’s the basics that someone often forgets. This is true in any profession once you progress to a certain level. The basics usually just come naturally, so people often don’t think that it could be something simple that is causing erros.

Javascript, for example, hates spaces. I knew this since I started programming Javascript. If you do not accomodate for them, it can and will lead to problems. Which is what I spent an hour trying to figure out last night.

See, the user can upload images into the gallery. The script replaces the background of a div with the image that was clicked. Pretty simple and clean, if I do say so myself. However, the user could upload images with just about any naming convention they wanted. This is what was causing the issues. The spaces in the names of the file that were uploaded. The script would come across this and crap out because I did not accomodate for spaces.

The solution was simple enough; replace the offending character with its HTML encoded equivalent, in this case using “%20”. After this was done server side, the script worked just like it should, and I commenced with beating myself about the head with my Javascript Bible book.

So the lesson of the day? If something isn’t working, and you know that it should be, go back to the basics. Are my quotes in the right place? Are there any offending characters that JS doesn’t like? Did I include the file with the correct path? These things will often be the cause of the issues and you will be greatly relieved that you spent the five minutes checking the basics (and often solving the issue), rather than wasting an hour ignoring the stuff you knew was right.

Cheers

Written by iGonzo

January 24th, 2011 at 10:39 am

Sort out your records – MySQL style

without comments

So I had a predicament. I had to sort out a list of records. Not amazingly difficult, I know, but the client wanted it sorted in a specific order. The Status (which is what I am sorting by) was displayed as text on the front end, but was a number in the database. So my task was to order this list by a non-sequential order that I can define.

As is usual, I played around a bit before I got it working. Then I started looking for a better (and non code based) way to go about it. The answer came in the form of the ORDER BY FIELD() MySQL function.

Usually, your ordered statement looks something like this:

SELECT * FROM where_ever ORDER BY Column1, Column2

Simple, but not helpful in this case. But adding in the ORDER BY FIELD command, our SQL statement looks like this:

SELECT * FROM where_ever ORDER BY FIELD(fieldToOrderBy,4,1,5,6)

So now when you display your records, everything with a “Status” of “4” will be first, “1” will be second, and so on. If there are more status values possible those will be put to the back of the order, as the pre-defined values take precedence over the rest of them.

You can also use text as an ordering value (which is a good thing sometimes), which will look like so:

SELECT * FROM where_ever ORDER BY FIELD(fieldToOrderBy,’Medium’,’Large’,’Small’)

The above will be helpful if you want to show all the Medium items first, Large next, and so on.

To sum up, before you do like I did and spend a lot of time going about things via server side code, re-read the MySQL (or whatever database system is you poison) documentation. You are almost guaranteed to find some useful shortcut to save time, code, and headache.

Cheers

Written by iGonzo

January 5th, 2011 at 4:35 pm

Posted in Database,Programming

Authorize.net and Classic ASP

with 2 comments

You know, a lot of people are moving off to other, cooler programming languages. PHP, ASP.NET, Ruby, all of these are seen as superior to Classic ASP; at one time the greatest language to use for dynamic data driven sites.

More often than I care to admit, I get asked “Why don’t you do it in [insert current language here] to do that? It’s easier”. True, but I have been using ASP for 11 years and am quite good at it if I do say so myself. Besides, as long as it can process XML, I see little reason to change our entire code base. Which brings me to the actual crux of this post: Using Classic ASP with Authorize.net.

More specifically, you can use any programming language with the Authorize API as long as you can process XML. But since I use primarily Classic ASP, that is what we have to use.

Authorize, in their wisdom, has seen fit to provide examples of accessing their API. One of these examples includes an ASP version. At first I totally ignored these, assuming that they could not accomplish what the client needed (which is integration with the Authorize CIM system). And so I toiled for many an hour creating functions and testing. Eventually I came across a problem I could not bull my way through, so I humbled myself and looked at their example code.

My first reaction was, of course, chagrin. Their ASP examples were performing functions that I needed. Precisely what I needed. *urg*. So, over the next 20 minutes, I functionalized their examples, added in our data points, and low and behold I got a positive response without any errors. I even tried to create errors and still the data went through.

Begin head banging on desk.

So then, after my headache went away, I grabbed every code example they had and was able to add in a whole mess of features and functions that not only added robustness to the site, but some serious value for the client. The transmission functions I created are able to take any form of XML request and sent it to Authorize and get a response back. After a quick check my reply data is added to the database and its on to the next thing: which for the client is making money (businesses are funny that way).

The lesson here, trolls and girls, is to not scoff at API example code. We can all look at the PDF of how to form this or that request, and what to expect back in response, but seeing a bit of sample code execute without errors is, in my opinion, a very uplifting thing to experience.

Now, if I can just figure out how this FBML crap, I think I’ll be golden.

Cheers

Written by iGonzo

November 5th, 2010 at 1:34 pm

The bane of JavaScript

without comments

So I just spent the last hour wondering why my GMaps implementation was not working on a particularly complex site. More to the point, I was wondering why it was working on the live site, but not on the testing site. That is, until I looked at the live site and saw it wasn’t working there either.

Uh oh.

So I started looking at the usual suspects when my JS isn’t working; forgot a semi-colon, misfiring functions, garden gnomes, etc. I then started looking at various doc-types, versions of jQuery and my GMaps API, even my content-type settings, and nothing was working.

Then it hits my like a lightning bolt: 90% of problems I deal with are from content. Specifically, content that has a lot of strange characters that HTML generally, and javascript in particular, doesn’t at all like. So I go through the dynamically rendered markers for my google maps’ points and find, in the 50 or so points I am rendering, there is one name that has an apostrophe in it.

Grrrrr.

A quick replace function in the code and, low and behold, shit starts working again. To make sure it doesn’t happen again I am now running the content through our nifty illegal characters replace function because, in the future, someone may enter a name or something that has a tilde character in it or something else equally strange.

The point is, boys and girls and others, that if you are dealing with javascript, especially if it is very large and complex javascript, be sure that there are no floating apostrophes out there.

On a side note, I am not against apostrophes  in general. I just believe that languages can do without them. I mean seriously; when was the last time you texted a properly formatted message to someone?

Thats what I thought.

K Thx Bia

L8r

Written by iGonzo

October 26th, 2010 at 1:06 am

Search Arrays without server code (sorta)

without comments

So it happens that I have a database field value that is a comma separated string of numbers. Classically, I would grab the field, turn it into an array, and search the array. Somewhat cumbersome and not really worth the effort of another function. That, and I would have to grab every record in the database, search through the applicable fields, and filter out the record(s) I need.

Me, being lazy, want an easier way to do it. Then I found this:

Then I came across this: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

All I have to do is construct a statement like this:

SELECT * FROM table_name WHERE FIND_IN_SET(‘thevalue‘,Field_To_Search)

You can also use a variable in place of ‘thevalue‘ if you need to search for, say, a passed parameter.

In the end, I was given a list of one or two records that I can easily do the rest of the work for, instead of grabbing every record in the table and filtering after the search.

For the lazy folks, this is quite a good thing. For the non-lazy folks, this is still a good thing. The above query not only executes faster, the code weight is greatly reduced and also runs faster (tests show about a 40% increase in speed and a 50% reduction in code execution time).

Final thoughts; this now leaves me more time to either work on something else, or go grab some food. I vote food.

Cheers

Written by iGonzo

October 15th, 2010 at 11:53 am

Posted in Database,Programming

Tagged with , ,

Check Yourself (and your code)

without comments

So I just spent a large-ish amount of time trying to figure out why, when I updated one data table from an XML feed, it updated a completely separate table that was not even related to the updated one.

The answer was on one tiny little line of code. That line of code executed a function in the other XML feed processor for the other table and dataset. The problem was that I copied the function and changed a few variables (since the feeds were almost identical) but didn’t go through and check to see if anything else was going on.

The lesson for today: Check your freaking code. If you want to copy/modify a function to use elsewhere, make sure that there aren’t any little items that would make for a not fun programming day.

Cheers

Written by iGonzo

August 20th, 2010 at 3:27 pm

Posted in Programming

Tagged with , ,