Computer Magic
Software Design Just For You
 
 

A new form of quick reference for programmers

October 4th, 2006

A very cool new feature that Google has now provided is its list of gadgets. These are premade tools that you can customize and drop right into your own site. There is weather, maps, even some games.

Since I am a programmer, I picked a programmers reference. This allows you to type in topics and the reference will use Ajax to lookup subjects that match your topic. It is pretty slick. Give it a try.

Ray Pulsipher

Owner

Computer Magic And Software Design

Case insensitive URLs with Apache

July 6th, 2006

On a recent project, the request came in to allow for case insensitive URL’s. The site is housed on a *nix box running Apache. Since *nix systems don’t support case insensitive file/folder names, it won’t work natively. To accomodate this request, we turned to our most complicated and obscure friend ModRewrite.

ModRewrite allows you to mess with URLs. You can redirect the requests internally in all sorts of interesting and creative ways. You can even do an external redirect (where the browser actually is instructed to make the request for the new URL). You can even take advantage of regular expressions for pattern matching.

Our project was in a folder (e.g. www.mysite.com/test). The client wanted to be able to put in (www.mysite.com/TEST) or (www.mysite.com/Test) or any other combination. To setup ModRewrite rules, you can either put them in the main http.conf file, or you can put them in an .htaccess file. One thing to consider though is that ModRewrite works from the current directory on. You can’t change the part of the path previous to your current location. This means that you can rewrite the index.html part of (www.mysite.com/test/index.html) but not the test part if you put the .htaccess file inside the test directory. To modify the test directory, you need to put the .htaccess file in its parent folder. This is important. If you don’t your rule won’t work or it will cause a 500 error. I fought with this for about 15 minutes before I banged my head against the screen and said doh in my most Homer like impression.

Placing the .htaccess file in the root of the web server, I can put in this rule…



RewriteEngine On
RewriteRule ^test/(.+)$ test/$1 [nc]

The first item (RewriteEngine On) makes sure that the ModRewrite engine is running. If it isn’t, the rule won’t work.

The next item is the actual rule. RewriteRule just says “the rule follows”. The ^ character says this has to match the beginning of the URL. Notice that I didn’t use the beginning slash (/). Depending on your RewriteBase this might change.

The rule says, “All URLs beginning with test, that have a slash (/), and then followed by anything will match”. The $ sign here means the end of the url.

Once we establish that something matches, we use the next part of the rule (after the space). This is where to redirect to. Default is an internal redirect and the browser doesn’t know any better.

If we have a match, we redirect to the test folder (notice all lowercase, this needs to match your actual case for your folder). After the slash, there is the $1. For every (???) section in the previous part of the rule, you get a $ variable. They are assigned in order. So, since we have (.+), $1 becomes whatever that matched. This is a wild card and allows us to shoot requests for (/TEST/about.html) to (/test/about.html).

The last part is options that are applied when matching. By putting nc in the brackets, we are saying “use a non case sensitive match” so TEST and Test will match test when we are checking for matches. This is what gives us our case insensitive matching.

One hangup with this rule is that it doesn’t deal with requests where a file isn’t specified (e.g. www.mysite.com/test/index.html v.s. www.mysite.com/test/). If you leave the file name off, it doesn’t know what to do since the $1 doesn’t equal anything now. I added a couple of rules to account for this. I am sure you could make it work right by tweeking the original rule some more, but this was easier for me and it works just fine. Here are the additional rules to handle the (/) issue.



RewriteRule ^test$ http://www.mysite.com/test/index.html [r=300,nc]
RewriteRule ^test/$ http://www.mysite.com/test/index.html [r=300,nc]

These rules are basically the same, except we aren’t looking for any old file name after the (/) character, we are specifically looking for no file name after the (/) character. We also have a second rule to handle no (/) character at all (www.mysite.com/test/ or www.mysite.com/test). Our default page is index.html, so we will redirect to there. In this case, we do a full redirect in which we send the browser a response of 300 (the r=300) telling it to request the specified URL. This is an external redirect in which the browser requests this URL, then is told to request a new URL. The previous rule does the translation in the server and the browser never knows the difference. Again, the nc option specifies non case sensitive matches.

If you have trouble making this work, make sure that your .htaccess file is in the right location. You can also try adding the (/) character in front of your rules (e.g. ^/test instead of ^test). I think this changes depending on how your rewrite base is set.

This could be modified to work for all URLs instead of just a test directory. I am sure that some one with more time or experience could find a better way to do this, but I had trouble finding good examples online for this and hope that this will help others avoid a little frustration.

Ray Pulsipher

Owner

Computer Magic And Software Design

Special Webhosting Offer for Netbunch and Webhostplus customers!

June 7th, 2006

Are the techs at your web hosting company absent? Check out or limited time offer for customers who are unhappy with their current hosting compnay.

50% off Web Hosting for the first year for customers unhappy with their current hosting service

Ray Pulsipher

Owner

Computer Magic And Software Design

Special Webhosting Offer for Netbunch and Webhostplus customers!

June 7th, 2006

Special Web Hosting Offer!

As a former NetBunch (WebHostPlus) customer, I understand the frustration and heartache that you are going through. The considerable down time and effort spent just to get minor things fixed was one of the motivating factors for us to offer our own web hosting services. Since offering web hosting services, I have noticed an increase in the amount of traffic from others looking for help either dealing with this company, or looking for a way out. Here is your way out.

For all current or former NetBunch, WebHostPlus, Mesopia, and DR2.net customers who are tired of crappy, slow, and unavailable service, contact me directly at rayp@cmagic.biz and get a steep 50% discount on your first year of service on our already competitively priced packages. Have you or are you dealing with another web hosting company that had crappy service? Send us your story and we will extend the same offer to you! We will even assist you in moving your service and get you up in running ASAP (additional charges may apply if you require code changes or custom web applications/databases moved). No anonymous ticket system. A real person behind a real e-mail. Even a real phone number for business customers!

We are all busy and have better things to do than to spend hours and hours dealing with an unresponsive hosting company. Contact us and let us save you time and heartache. Let us manage your hosting experience with one point of contact and one bill. It is our mission to keep you from calling.

For more details on the packages we offer, visit our page at: Web Hosting

Remember to contact me directly to receive your 50% discount before paying.

Ray Pulsipher
rayp@cmagic.biz
Owner
Computer Magic And Software Design

PHP – Object References Vs. Object Copies (Pass by reference or pass by copy)

June 2nd, 2006

An important concept in programming is pointers. Even though we don’t directly use them in most modern languages the way we used to (think C/C++) they still come back to haunt us. In many modern languages they are referred to as references. References aren’t quite pointers, but they do resemble them enough to illustrate the concept (note that C/C++ has both pointers and references!).

What is the difference between a pointer and a reference? A pointer can be typed or non typed. A pointer can point to an arbitrary location in memory and invalid pointers (pointing to the wrong spot) is one of the causes of segmentation faults and blue screens in microsoft windows in the past. The pointer is very versatile, but also very dangerous.

A reference is similar to a pointer, but not quite. Think of a reference as a pointer to an existing varaible (not just some arbitrary pointer). See how it is similar? Some people get confused easily here.

In our case, we are only going to talk about references and how they work in PHP. References are important because they can affect the speed and functionality of a script. When a function in PHP is called, the parameters supplied are generally passed by copying the information. This means that if you change data passed into a function, when you leave the function the data will not have changed. Look at this example.



<?php $a = "Testing..."; print "Variable = $a...<BR>"; function ChangeString($str) { $str = "String chanaged..."; print ">>> Local variable = $str...<BR>"; } ChangeString($a); print "Variable = $a...<BR>"; ?>

This code shows the following output.



Variable = Testing...... >>> Local variable = String chanaged...... Variable = Testing......

Notice that the function modifies the string passed to it, but once we are done with the function and return to the main script, the varaible is untouched. This is because when we pass the variable to the function, it actually makes a new copy. This copy is also set so that is applicable only within the scope of the function (local scope). Some might think that this difference in scope is the cause of the copy. That is not the case. While scope is an important concept, it is not the reason the variable doesn’t get changed, the fact that it is copied is. Lets illustrate the point with some more examples.



<?php $a = "Testing..."; print "Variable = $a...<BR>"; function ChangeString(&$str) { $str = "String chanaged..."; print ">>> Local variable = $str...<BR>"; } ChangeString($a); print "Variable = $a...<BR>"; ?>

This is the SAME script, with just one minor change. In the definition of the function, we add the & character. This character means get a reference to the passed variable. In essense, we are now pointing to the original variable instead of making a copy. Here is the output now.



Variable = Testing...... >>> Local variable = String chanaged...... Variable = String chanaged......

Since we are now pointing to the original variable rather than making the copy, if we change either one, they both change since they both point to the same memory. Here is the same example without the function call. Here we will use simple variables to demonstrate the same issue. Note that both demonstrations (copying and referencing) are demonstrated in the same script; most of the script consists of print statements in order to show you whats going on.



<?php print "The copy example...<BR>"; $copy_a = "A value"; print ">>> A = $copy_a...<BR>"; $copy_b = $copy_a; print ">>> B = $copy_b...<BR>"; $copy_b = "B Value"; print ">>>>> After changing copy_b...<BR>"; print ">>> A = $copy_a...<BR>"; print ">>> B = $copy_b...<BR>"; print "<BR>----------------------------------<BR>"; print "The reference example...<BR>"; $ref_a = "A value"; print ">>> A = $ref_a...<BR>"; $ref_b =& $ref_a; print ">>> B = $ref_b...<BR>"; $ref_b = "B value"; print ">>>>> After changing ref_b...<BR>"; print ">>> A = $ref_a...<BR>"; print ">>> B = $ref_b...<BR>"; ?>

The output looks like this:



The copy example... >>> A = A value... >>> B = A value... >>>>> After changing copy_b... >>> A = A value... >>> B = B Value... ---------------------------------- The reference example... >>> A = A value... >>> B = A value... >>>>> After changing ref_b... >>> A = B value... >>> B = B value...

When simply assigning a variable to another variable, you are actually copying that value. When you use the & sign, you are pointing the second variable to the first so that they both point to the same value or memory location. This means that changing one will change the other.

Some languages copy everything unless specifically told to reference. Others copy base data types (integer, float, string, etc..) and automatically reference object types (meaning you can assign variables to your objects like you are copying, but it will in the background reference it for you). Generally you just get used to how your language works and use it accordingly.

It looks like PHP changed how they do things between versions 4 and 5. In version 4, you have to explicitly reference everything if you don’t want to make copies. In PHP 5, it is smart enough to automatically pass references when you are working with objects. This created quite a headache when we tried to run a script written for PHP 5 on a server that had PHP 4 installed. The script had to be basically rewritten and lots of & symbols introduced. Why did PHP change things you ask? Because generally you want objects passed by reference. When adding the extra object oriented support in PHP 5, they decided to conform with most other languages in this respect. It was a good choice, especially since most people don’t have to worry about running new scripts on old servers. All old scripts that specifically use references should still work in the newer PHP versions.

Earlier I mentioned performance. Everytime you copy a value, a new chunk of memory has to be allocated. For base types (integers, etc..) this isn’t as big a deal. For Objects that may have lots of data, this can create a significant ammount of overhead. If you need a copy, then there is no getting around it, but some times you can spee things up quite a bit by using references.

On one project I was working on, a string variable held the whole contents of the current page (think 20-30K!). We sent this string to a processing function. At first, we used the normal method of passing in a copy, and then setting the string to the returned value:



$str = "lots of data"; $str = ProcessString($str); function ProcessString($s) { $s .= " more data"; return $s; }

This worked fine. The data from the $str variable was copied when passed to the function. The function would process the string, then pass back the processed string. Again, when passing or returning the resulting string, a copy is made again. So, our $str variable will hold a copy of a copy when we are done (or even a copy of a copy of a copy depending of if copies are made inside of the processing function!). You can see how this will add up.

By using a reference, you can modify the variable that is passed in directly. You ONLY want to do this when it is ok to modify the variable directly (not all the time!). By saving the copying where appropriate, you can speed up your code. The same code using a reference would look like the following. Notice that you don’t need to return a value now since the value being passed can be directly modified.



$str = "lots of data"; ProcessString($str); function ProcessString(&$s) { $s .= " more data"; // return $s; }

Ray Pulsipher

Owner

Computer Magic And Software Design


Home | My Blog | Products | Edumed | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
This page has been viewed 830723 times.

Copyright © 2005 Computer Magic And Software Design
(360) 417-6844
computermagic@hotmail.com
computer magic