Web Development

Web Development Tips & Tricks, the things that you don’t want to figure out.





Archive for the ‘Phoenix Development

PHP – Unsetting a Cookie Array

Sunday, April 29th, 2007

Hi everyone,

Most of what I’ve been doing recently I can’t blog about because it’s not complete, and who would want a half written blog?

What I am blogging today is a little loop that will unset a particular array in the Cookie array. If this sounds confusing, I’m not surprised. All the cookies are part of the Cookie Array. You can make subarrays within the Cookie arrays. For instance:

$nextWeek = time() + (7 * 24 * 60 * 60);
setcookie(“clothes[shirts]“, “blue”, $nextWeek, “/”);
setcookie(“clothes[shirts]“, “black”, $nextWeek, “/”);
setcookie(“clothes[shirts]“, “red”, $nextWeek, “/”);

And then, to make it a little more complicated, suppose we added another part to that array:

setcookie(“clothes[pants]“, “small”, $nextWeek, “/”);
setcookie(“clothes[pants]“, “medium”, $nextWeek, “/”);
setcookie(“clothes[pants]“, “large”, $nextWeek, “/”);

Now, lets say we wanted to just unset the “pants” subarray. Here’s a loop that could do it:

$subarray = “pants”;
if(isset($_COOKIE['clothes']) and is_array($_COOKIE['clothes']))
{
foreach($_COOKIE["items"][$subarray] as $key => $value)
{
$result[$key] = setcookie(“items[" . trim($subarray) . "][" . trim($key) . "]“, “”, time() – 3600, “/”);
}
}

I may add on to this post later and post an unset function.

-Kerry

Dynamic Content and Static URLs

Saturday, April 14th, 2007

SEO at its best!

Search engines will look at Dynamic pages as their own pages (with their own query strings), and index them as such. The advantage to having a Static URL is you can use keywords in the title which well help with your SEO. For instance, if you had an article about global warming, you might want the name to be “http://www.yoursite.com/global-warming.php”. But you don’t want to have to actually create a static page for each of those URLs.

There are two ways of doing this, one better than the other. What I had been doing for a long time is make a static page, then including the dynamic page and passing variables to it. Something like:

<?php
$articleid = 27;
include(“dynamic-page.php”);
?>

The dynamic page is coded to take that variable and then it displays the page. This is definitely better then redoing the code every page, but it can be improved.

.htaccess is the key to this solution. It can only be used on Apache servers as far as I know (sorry to those of you who have a Windows Server). Regular Expressions are also used in .htaccess, and will be in this example. The basis of this solution is you use .htaccess to redirect static links to a dynamic page, but when you redirect the links address doesn’t change. Here’s an example of how you can do that (put this code in your .htaccess file).

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)-a-([0-9]*).php$ /dynamic-page.php?articleid=$2 [L]

Ok, basically says any link to .php that ends with “-a-” (number) “.php” will be sent to your dynamic page, with the query string “articleid=” (number). So, lets say you went to “global-warming-a-27.php” it would redirect the page to “/dynamic-page.php?articleid=27″. Now your page can use that ID to call the right article from the database or wherever you’re storing the article.

That’s it! It’s as simple as that. This will allow you to create static urls, so Search Engines can find them and people can book mark them, but you don’t have to create any extra code.

-Kerry

Another Regex

Tuesday, April 10th, 2007

Hello All,

You may have noticed that I’ve been writing less posts recently. That’s because of two reasons: I’ve been doing a lot of other activities, and the project I’m working on is a long one. I usually write about something that I’m working on every couple of days. I’ve been building a shopping cart, so I’ve be having several posts about Paypal integration, setting up various accounts and so on.

Today’s post is a small one, just another regular expression I made. Here it is:

^[-+](?:[0-9]+(?:\.[0-9]{1,2})?|(?:\.[0-9]{1,2}){1})$

This will match amounts that are lead by a + or – symbol. For example, it will match these: “+3″, “+599.99″, “-4.45″, or “-.5″. It will not match “3″, “-.”, “+3.356″ etc.

I hope this will be of some benefit to you.

-Kerry

Reading XML – XSL or PHP?

Friday, April 6th, 2007

XML, or Extensible Markup Language, is probably the most versatile language there is.

Since you make your own tags, and then get readers to use them, you can use the data in them for pretty much anything. Well, today I was making my php reader display a bunch of URLs based on a category they were in. They also had special attributes and so on, all of which was contained in an XML file.

As I was looking at my sitemap, I realized that it had formatting, but it was an XML, and XML files can’t format themselves. So, I found out that it called an XSL (or Extensible Stylesheet Language) file that gave it formatting. XSL is a language used to make XML in HTML or XHTML. Call glorified HTML or XHTML (has in built functions to read XML). You can make entire pages, or parts of pages in XHTML and call in the data from an XML sheet.

If this is the language, why should you use the roundabout method offered in PHP? It took me some time thinking and wondering the advantages of each. Realise, I don’t know XSL (I started learning today), so I don’t know its full functionality. I do know, however, that you can see the XSL file and you can see the XML file. There’s a difference.

PHP code is server side, meaning completely executed before it reaches you, so you can’t see it. This includes the call to the XML file. This means you can display data based on an XML file without letting the surfer know that you called an XML file. This is a security feature, which I kind of enjoy for my current purpose of the URLs.

Now, I assume that XSL has a lot more functionality than PHP, seeing as how it is a language specifically to convert XML into HTML or XHTML. PHP is a complete programming language. It actually didn’t have a lot of it’s support for XML until PHP5 came out.

One more thing while we’re on the subject of URLs. I needed to grab just the domain name out of a url. For example in “http://www.google.com” I want just “google.com”. In the case of “http://mail.google.co.uk” should match “mail.google.co.uk”. I decided to see if I could put my regex skills to work, and I came up with this: “(?:www\.)?([^.\/]+\.(?:[a-zA-Z]+\.)*(?:[a-zA-Z]{2}\.[a-zA-Z]{2}|[a-zA-Z]{2,4}))” which matches both of those examples. You simply have to grab the return value (there’s only one) and it will have the right result.

-Kerry

Courtesy

Monday, April 2nd, 2007

Hello once again,

Today’s blog is about courtesy. I’ve been trying to get the logo for my company started so I can make the site around it, which is one of the first steps in the design.

I found the logo I want on deviantART, which is a great logo. A friend of mine touched it up, and I loved it, so I thought it would be a pretty simple deal asking permission from the artist. I sent him a note, asking him if he would mind if used it as my business logo.

A couple days go by… no response.

So, I went on and checked to see if he was an active user. Yep, he responded to comments within the last day or so. I thought, maybe he doesn’t check messages, but comments. So, I left him a comment.

A week and a half go by… no response yet again.

I check, he’s still active. I decide to leave him another. 6 days later and it’s today. I’m still waiting for a response, and I sent him another note. I don’t see what the problem is, if he doesn’t want me to, he could simply say so. If he wants something for it, he could say “yes, but unfortunately not for free” or something like that. Or, he could just say yes. Simple communications, definitely doesn’t take the 19 days I’ve been waiting.

Aside from that, you’re be getting more on regular expresisons in a day or so, so I hope you enjoy!

-Kerry

Site Maps (Free Script)

Thursday, March 8th, 2007

All praise the glory of sitemaps!

A sitemap can be two things. There is the sitemap page that you see on someone’s website, which can be just some unordered lists or indenting, maybe a touch of CSS (Apple Sitemap), or you can do a little more with it (Download.com Sitemap). You can be completely original and make something really cool, like Websites in a Flash Sitemap. These show you all or the majority of pages on a website, and it is sometimes easier to navigate with them.

Then there is the xml sitemap that helps your SEO. These pages are a bit more complex and have a strict protocol which must be followed. Sitemaps.org goes over this protocol with detail, probably more than you need, if you plan on creating them by hand (I’ll go over other methods below). These sitemaps show crawlers what other pages there are on a site and helps them get indexed, as long as they are in the correct format. It can also tell Google other information, like it’s priority in relation to your other files.

You should submit your sitemap to search engines, obviously the largest being Google. Part of the webmaster tools (an issue for another blog) includes checking out your sitemap and making sure it can be read ok. If you want to submit your sitemap to Google, go ahead and click here. There are two methods that I know to create your sitemap xml file (other than hand coding, and who wants to do that these days?):

  1. Use a website to scan and create your sitemap xml file.
  2. Use a program to create them.

I tried and online website, and I don’t remember why I didn’t use the file, but I ended up with a downloaded program and loved it, so I stuck with it. Here’s a site if you want to try one: http://www.sitemapspal.com/.

The program I got is called GSiteCrawler. If you’ve noticed I’m mainly centered around Google, though I do suggest you put some time to get your sites listed in other search engines, the main ones at least.

Here’s the home page for GSiteCrawler: http://gsitecrawler.com/. This program has a lot of added functionality, you can choose what type of files you want it to pickup, you can exclude files, it has an ftp program so it will automatically upload your sitemap file to your site, you can set it on auto so it will do it periodically, it will help you sign up with the Google and Yahoo tools you submit your sitemap, it will create statistics and much more. The biggest thing is it’s free, and I’m one of those annoying frugal types and hate to buy anything I don’t have to, and I still hold there is a free program that is as good or nearly as good for any paid program (prove me wrong, I dare you).

Now, onto what prompted me to write this post. I’ve always been annoyed by any script or any page where you have to update something twice. If you change one thing, then it should change everything else relevant. Poor programming shows otherwise, if you don’t use includes and you add a page onto your site, you have to go through every page of your site and add another link to your nav bar, and that’s just annoying.

My point being, you shouldn’t have to update your xml file and your sitemap file every time a page changes. Well, how would you fix this? You have to get one to change the other or be based on the other (it reads it and generates a page). I’m sure there are scripts like this out there already, and I just couldn’t find them, but I decided to make my own. I’ve spent pretty much the last 6 hours on it, way too long I know, but I wanted to get out all the bugs and I wanted it to handle all situations.

Realize, if you have a special sitemap like the Websites in a Flash one above, this won’t work. The sitemap xml file has very little data for you to be creative with, all it contains is the link location, possibly the priority of the files, the frequency it was updated, and possibly the last time you modified the page. That’s not much info to move the links around, categorize them, etc., with a automated script. The only real way to separate them is by their link names and the folders they’re in, and then you can use css and do all sorts of things (like you could make a separate div with a box for each folder, and the links are in that div).

Well, I managed to create that script. Right now it can create your sitemap off of two pieces of information, the link to your sitemap and your website link, and voila, it will create it all for you. It automatically gets the name of each file in relation to it’s file name, for example. If you have a page “about-us.php” it will turn it into “About Us”. Right now this only works for -s, but it also has the ability to rename files and folders of your choice. You can say that you want “aboutus.php” to display “About Our Company” or anything else, same with folders.

Here’s the site I was working on: http://www.viewyourrights.com/sitemap.php. If you’re interested in the script and would like to check it out, leave me a comment with your e-mail and I can send you the files. I’ll also help you with changing the structure of it if you want (using CSS to make it look cooler).

Catch ya on the flip side,
Kerry

The Rise From The Ashes

Wednesday, March 7th, 2007

Ok, a dramatic title but not really anything to back it up, even though today is kind of symbolic.

Phoenix Development, my company, finally got a website up and running. Even though it has a temporary layout and is under construction, it has four pages up and is functional. You can see it here: http://www.phoenixdev.net.

Anyway, this blog will probably mainly be things regarding web development, design, hosting, SEO or something in the web line. But just in case, be prepared for that random blog, might hit you when you least expect it. They will also probably be much longer than this, and the links off this page will probably increase, and there are many changes that you will see in the future.

-Kerry