Web Development for 2010

Alexander Dickson - Web Developer covering PHP, jQuery / Javascript, XHTML, CSS, more

Join 2 arrays the easy way in PHP

Published on Tuesday, 9th March 2010.

Today I came across a problem that I do often: I need to join 2 arrays together. My usual friend here is array_merge(). But I decided to use something I had seen a lot in other people's PHP code, but had never tried myself.

I had an array of numbered keys to values and I wanted to prepend a new value without affecting the keys. First I used array_unshift(), but it reset my keys. No good, as they were numerical Ids. Then I turned to array_merge(), which also scrambled my keys.

I decided to use something I have seen in the wild, but never tried myself. That is, to concatenate them using the plus (+) operator.

$joinedArray = $oldArray + $newArray;

For those people wondering why it works, apparently it is defined in the interpreter that when using + where all operands are arrays, it does what you may expect... join them all together! I guess it's another random thing a PHP developer should know!

0 comments

Form security - submitting forms to themselves in PHP

Published on Wednesday, 3rd March 2010.

Often, well in fact for me nearly always, you want a form to submit to the page it is on. Here is some standard markup (with some PHP).

<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" method="post" > <fieldset> <legend>Contact Me</legend> <label for="email">Email:</label> <input type="text" name="email" id="email" /> <button type="submit">Submit</button> </fieldset> </form>

I've always added the htmlspecialchars() so that if anything fishy is appended to the URL, it will be encoded safely.

However, I've been experimenting with omitting htmlspecialchars() and it still seems to be safe, because the browser is encoding characters from the URL. For example accessing form.php?hack=" onsubmit="alert('xss') will make the form's action attribute /things/?hack=%22%20onsubmit=%22alert(%27xss%27). Not elegant, but not dangerous either (I think).

So is it necessary to use htmlspecialchars() ? I still will, and I recommend you do too. It takes 4 seconds to implement, and it will safeguard you against any client that does not automatically encode the special characters in the URL.

2 comments

Learning the C programming language

Published on Sunday, 21st February 2010.

In my last post, I mentioned that I was going to undertake learning iPhone development. Whilst looking at the many online references, I decided I would go back a few steps and learn C before I moved onto Objective C.

C is quite similar to PHP in terms of syntax. That was the easy part. The hard part is obviously going from an interpreted dynamically typed language (PHP) to a compiled static typed language (C). So far, I have learn a lot of the syntax and basic ideas, but I'm finding it difficult learning how to program with a more low level language, with minimal helper functions.

I have also ordered a book, which seems to be a pretty definitive guide to C, that will hopefully get me on the fast track to getting the hang of things. It's called The C Programming Language, and by the looks of things it should help me a great deal.

Also, in determining where to go from "Hello World", I did come up with a good question to post to Stack Overflow, which as returned some interesting answers and ideas. I haven't chosen a suggestion from the answers, at this time, but once I'm more confident, I will definitely undertake a learning project.

I am enjoying the learning experience and hopefully can share some projects and tips over 2010.

0 comments

iPhone development

Published on Tuesday, 19th January 2010.

I have to confess something. I learnt the basics of JavaScript and PHP about 6 years ago. I have not learnt a new language since then. Besides the mentioned languages, all I did know were Classic ASP and Visual Basic. Luckily for me though, I do not remember much of the syntax of those latter languages.

I've decided, that starting tonight, I'm going to download the iPhone Software Development Kit and begin the treacherous road of learning a new language, Objective C. I'm all set now, with my iPhone and Mac Mini to take the plunge into Mac development.

I have no idea at this stage what sort of app I should make, and it seems most voids have been filled already (except for the voids Apple won't allow to be filled). I'm open to suggestions!

If you're a seasoned iPhone developer, and would like to give me any pointers, feel free to leave a comment or contact me directly.

2 comments

Love for my iPhone users

Published on Sunday, 10th January 2010.

I've spent some time this afternoon making my site more friendly for iPhone users. I also fixed a bug from my last roll out of changes: namely if tabbing through the comments field, it would automatically add http:// to the URL field (even if you didn't want to enter a URL).

A cool thing about the iPhone is that it supports HTML5 input element type attributes. This is especially cool because of the iPhone's on-screen keyboard. Now when entering the email into the comment input, the iPhone will show a slightly modified keyboard, that has a quick touch @ symbol. This is very cool. The same thing also works with the URL - it will show a quick .com button. This makes it much easier to enter the necessary details.

The problem however is I did not want to enter these values in my HTML because I am using XHTML 1.1 and it would not validate. At first I tried to change the attributes using jQuery, but it threw an exception telling me I could not change the type attribute. This sort of made sense, as I could see all sorts of confusion from changing type from text to checkbox etc... So I came up with a quick solution, and that is, detect for the iPhone, and add another input element directly beneath it, modify it's type and then remove the original. It ain't so pretty, but it works.

Another thing I did is detect the iPhone using user agent sniffing (more on that later), and display a better format site using nothing but CSS. Basically I hide the top and bottom strips and the sidebar, and jack up the font size so it's readable without horizontal scrolling (something that makes me feel ill). The good thing about sniffing for the iPhone user agent is that it would seem even more difficult for anyone using an iPhone to fake this, even though it could be considered that my visitors probably could much easier, being developers.

If you have any more recommendations on how to make this site more iPhone friendly, don't hestitate to let me know!

0 comments

About

I'm a web developer from the Sunshine Coast, Australia. more »