Things to consider when moving from Apache to LihghtTPD
September 6, 2007
1. Do you use some Apache MODs?
If you do, see if there is are alternatives for LightTPD. If there are none, you’re stuck with Apache
2. Do you use underscores in your domain/subdomain names?
If so, you’re out of luck again. LightTPD does not allow underscores in the domain names.
3. Do you have the time to do all the configuration rewriting?
Configuring an already existing website may take some time. The mod_rewrite syntax is different for example. Although much can be done by search and replace, there are some subtle differences, like LightTPD not offereing an alternative to QUERY_STRING(you can do this but it requires some thinking).
Another thing to take into account is that the parser errors are very confusing. I always get “the parser somehow failed at line x near y”. Besides that, it is very picky. One configuration file did not work because there was a new line between the name of a variable and a bracket. So white space matters.
4. Are you ready to lose support?
When you run into a problem, it is much harder to find solutions. You may need to post it on the lightpd forums and wait for an answer.
My advice? If Apache is a problem for you, it is better to move only images/css/other media to a LightTPD server. For example serve your php files from yourdomain.com(Apache) and your images from images.yourdomain.com(LightTPD). You’ll get rid of much configuration work this way.
As a closing word, I did not see much improvement after moving to LightTPD. Maybe I got something wrong, but for me it just did not worth it.
Some thoughts on MySQL optimization
May 20, 2007
First, the BENCHMARK function. It can be used to run a command a number of times.
e.g. SELECT BENCHMARK(1000, “SELECT COUNT(*) FROM categories”)
The EXPLAIN keyword, followed by a SELECT query, gives info for that query. I have to dig more into this.
Analyzing your tables seems to be very important. It re-creates keys, indexes or something like this. Not sure, but someone tells that it improved his queries 1000 times. Well,
mysqlcheck -Aa -uroot -p does it.
Use persistent connections to avoid connection overhead.
Make sure all your foreign keys are indexed.
Make sure you foreign keys match the type and size of the main keys.
Use the smallest data type/data type size possible.
Things you did not knew about google search
January 26, 2007
A little FAQ on google
Q: How do I search for a word I do not know how to spell?
A: If you know part of it you can write it followed by “*”(E.g. you can write literat* if you do not know to spell literature). As an alternative, type it wrong. Google will often suggest the right variant.
Q: How do I learn what an acronym means?
A: Define: Acronym(e.g. define: NASA). Note that this works for any kind of definitions
Q: How do I optimize my web page for google search?
A: The everlasting question… Do a search for “Nigritude Ultramarine”, “Mangeur de Cigogne“, Schnitzelmitkartoffelsalat or Gepardenforellen. There were search optimization contests on this keywords, so the pages in top are some of the most optimized on web. View their source and learn. The bad news is that you will soon see that they are on top because of a miryad of sites that refer to them. So the morale is that you must make people link to you, this being the only think that really counts in SEO. What really surprised me is that when searching for “search engine optimization”, the company on top has a table design, a flash menu and lots of other things considered bad practice in SEO.
Q: How do I exclude some words from my search?
A: Use “-”. For example, if you want to search for book references that are not from amazon, try The book I want -amazon
Q: How about searching for multiple terms?
A: First there are the “”. You can quote a string to be sure the terms in it follow one another, i.e. to search for a phrase. Then, you can always use AND or OR. For example “phrase 1″ OR “phrase 2″ will search for pages that contain either “phrase 1″ or “phrase 2″.
Q: How do I search for a range?
A: Use .. .To search pages that refer to the years between 2000 and 2007, search for year 200..2007
Q: How can I find vulnerabilities with google?
A: There are tons of ways. Here are a few:
“A syntax error has occurred” filetype:ihtml - will catch most informix errors
“Warning: mysql_query()” – will catch database problems in php/MySQL pages
“intitle:index-of last-modified private” – will find pages that permit directory listings
“powered by webcamXP” “Pro|Broadcast” – will find webcams
something “has quit” “has joined” filetype:txt – will find IRC logs
Q: How can I have fun with google?
A: Well… depends on what do you think “fun” means. Something I consider funny is typing the start of the phrase and let google continue i, e.g. “The best day in my life was”, “If I had more time, I would” or “Before I die, I want to”
If you liked this post, I have some sites that cover this kind of information much better:
The google guide
Directory of google hacks
Easy and fun to read book about google
These are some notes I took while watching a movie from microsoft. If this notes aren’t enough, get it here
1) Install SQL Server Express Advanced
2) Make sure that you include the installation of Full-text Search(not included by default)
3) Disable user instances, either by unchecking it at setup or by running
sp_configure ‘user instances enabled’,0
4) Make sure your database has full-text search enabled, by checking the “use full-text indexing” in the “Create new database” window or by accessing the properties window for an existing dabase, Going to “Files” and checking the “use full-text indexing” option
5) With your database active, run
CREATE FULLTEXT CATALOG MyFullTextCatalog
to create a full-text catalog.
6) Make sure that the tables that you want to be indexed under full-text search have a unique, non-nullable index.
7) Create your unique indexes as follows:
CREATE FULLTEXT INDEX ON Production.ProductReview(Comments)
KEY INDEX ui_ProductReview ON MyFullTextCatalog
WITH CHANGE_TRACKING AUTO
As you can see, you need to specify the catalog, a unique, non-null-able index and the name of the column to be indexed.
Slightly more complicated, you can create an index on a BINARY filed that contains files, like word documents. The code follows:
CREATE FULLTEXT INDEX ON Production.Document
(
Document
TYPE COLUMN FileExtension
LANGUAGE 0×0
)
KEY INDEX ui_ProductionDocument ON MyFullTextCatalog
WITH CHANGE_TRACKING AUTO
You can see that you have to have a column (in this case FileExtension) that specifies the extension of the document (e.g. .doc).
The LANGUAGE specifies the language in which the document should be parsed. 0×0 stands for default locale
Notes:
- run select * from sys.fulltext_document_types to see the document types that can be indexed.
- see http://msdn2.microsoft.com/en-us/library/ms187317.aspx for a detailed explanation of CREATE FULLTEXT INDEX
Test it out with something like
SELECT Comments FROM Production.ProductReview WHERE CONTAINS(Comments, ‘ “learning curve” ‘)
To learn more about the CONTAINS predicate, see http://msdn2.microsoft.com/en-us/library/ms187787.aspx
In a nutshell you can do stuff like:
- grouping word with “”, as in the example
- use wild-cards (e.g. compu* will match computer)
- use AND, OR, NOT
- the word NEAR (e.g. ‘”computer” NEAR “software”‘)
- inflexions, e.g. WHERE CONTAINS(MyColumn, ‘FORMSOF (INFLECTIONAL, “happy”)’) will return matches like happy, happier, happily
- is about, that will permit modifying the weight of a result based on the keywords found. E.g. WHERE CONTAINS(MyColumn, ‘ISABOUT (”computer” WEIGHT(0.5), “software” WEIGHT (2.0), “development” WEIGHT (10.0) ) RANKMETHOD INNER PRODUCT’)
CSS layouts to get you started
January 19, 2007
http://www.mitchbryson.com/css-templates/ – some nice templates to start with when creating a website. Sweet
Very usefull CSS Stuff
January 19, 2007
I stumble upon an article on digg, that presents some very nice css. I’ll summarize as I see fit. Probably is better for you to see the original at http://duggmirror.com/design/53_CSS_Techniques_You_Couldn_t_Live_Without/
1) Menu tabs – http://exploding-boy.com/images/cssmenus/menus.html
2) Collapsible tables – http://icant.co.uk/sandbox/footercollapsetables/
3) Drop shadow for pictures – http://web-graphics.com/mtarchive/vsds.html
4) Lots of round corners techniques, with and without pictures – http://www.smileycat.com/miaow/archives/000044.html
5) Block hover effect – http://www.smileycat.com/miaow/archives/000230.html
6) CSS image preloader – http://specere.net/blogs/?p=4
7) Form styling with CSS – http://www.smashingmagazine.com/2006/11/11/css-based-forms-modern-solutions/
Useful things for tables – http://www.smashingmagazine.com/2006/12/29/css-based-tables-modern-solutions/
9 ) Labeling zip, pdf, doc links – http://webdesign.maratz.com/lab/pdf_links_labeling/
Popup alternatives using html, css and javascript
January 11, 2007
As a web programmer, I hate when the popup blocker decides to block my popups. My clients hate it even more. So, let’s see another way to do it. The method is based on iframes.
1. Put the following functions in your<head> tag
function showPopup(hldr,x,y,w,h,url)
{
holder=document.getElementById(hldr); //get the div of the holder for your pop-up
holder.style.display=’block’;
holder.style.left=x;
holder.style.top=y;
holder.style.width=w;
popup=document.createElement(’iframe’);
popup.setAttribute(’height’, h);
popup.setAttribute(’width’, w);
popup.setAttribute(”src”, url);
holder.appendChild(popup);
holder.innerHTML+=’<button onclick=”closePopup(this.parentNode)”>Close</button>’;
}function closePopup(obj)
{
obj.innerHTML=”;
obj.style.display=’none’;
}
No rocket science here. I just get the div and insert an iframe in it.
2. Create you placeholders like this: <div id=”div1″ class=”popup”></div>. The popup class may contain any styling you want, but make sure it has position: absolute set.
3. Whenever you want to open a popup, call showPopup, e.g.:
showPopup(’div1′,’110px’,'10px’,'200px’,'200px’,'test1.html’) ;
That’s it! You now have a popup that cannot be blocked(at least unintentionally). Too simple? Should be.
Common sense programming
October 17, 2006
In this post I will talk a little about some common sense in organizing your code. There is nothing brilliant in this post, yet common sense seems to be the first thing we lose whenever coding something a little complicated. First, any code structure falls in one of the following:
- sequential structures
- logical structures (if, case)
- repetitive structures (for, while, do)
- others (goto, recursion, return )
I’ll talk a little about each
1. Sequential structures
Most of your code is executed once in a while. Yet even this can be confusing.
First, make sure that whenever the order of the statements counts, this is obvious. Let’s take an example:
calculateMarketingIncomes();
calculateSalesIncomes();
getCustomerData();
Now, suppose the sales incomes can be calculated only after the marketing ones (probably, the first procedure initializes some variables that are needed for the second). More, the customer data cannot be accessed until the sales incomes are calculated. It is obvious that the code sequence does not give a clue about this. There are a few options you can consider in cases like this:
- give better names: you can rename calculateMarketingIncomes to InitializeDataAndCalculateMarketingIncomes. Clumsy name? Maybe you should break your code into 2 procedures, InitiailzeMarketingData and CalcultateMarketingIncomes
- have some parameters that make the sequence logical. Have a value returned and use it in the next procedure. That should make clear that the order is important
- if and only if you cannot change the structure of the code, have comments that explain the importance of ordering the code that way
In other cases, the order does not matter for a certain step. Yet,the statements should be grouped logically. Consider the following bad example:
object a1=new object();
object a2=new object();
object a3=new object();
a1.doStuff();
a2.doStuff();
a3.doStuff();
a1.clean();
a2.clean();
a3.clean();
The bad thing is that the code for each object is interlaced with the others. When debugging a1, you have to look at 3 times the number of lines that you normally should. A better version is:
object a1=new object();
a1.doStuff();
a1.clean(;
object a2=new object();
a2.doStuff();
a2.clean();
object a3=new object();
a3.doStuff();
a3.clean();
This way, you will have to deal with less lines when interested in one object and it will be also easy to separate the code into routines if needed.
Logical structures
Logical structures are needed whenever one needs to execute a certain piece of code depending on certain factors. First, consider using routines for checking. Instead of
if ((ch>=”a” && ch<=”z”) ||(ch>=”A” && ch<=”Z”))
consider
if(isChar(ch))
where isChar is a routine defined elsewhere.
Another thing to watch for is testing for the logical condition. Instead of testing for not not false, test for true.
Whenever you have nested if conditions, make sure the normal path is easy to follow. Try to put the usual case in the if clause and the unusual in the else. This will improve both readability and performance.
Repetitive structures
First of all, make sure you use the logically right structure. It is known that in languages such as C++ you can achieve with a for everything you can achieve with a while or do…while. Yet there is no reason not to use while or do…while if that improves readability.
Try not to nest more than 3 repeatitive structures. Besides the big performance penalty, it is known that our brains have troubles in understanding them.
Beware of cross talk. It is common to use variables as i,j,k to walk through repetitive statements. Yet it is common to manipulate j instead of i. Try to give the variables more meaningful names whenever you have nested control structures.
Others
Beware of gotos. gotos are not evil, but they really decrease your code readability. Use them only if no other options are available. And think it through before deciding that you have no more options. Cases when you really need them are very, very rare.
Beware of recursion. It is an elegant solution, yet very resource intensive. First, make sure you have an exit condition. Then, make sure that there will be no stack overflow. And, most important, consider not using it. The usual examples, like computing a factorial using recursion, are plain wrong. You can do these operation with a loop and you will save lots of resources (by eliminating the overhead of a function call) in so doing.
If your language supports foreach or any other kind of looping through array structures, use it! It increases readability.
Not that everything in this post may be obvious. Yet remember these advices when you are deep into your source code.
Kind of names to avoid
October 16, 2006
- Misleading name or abbreviations. Some time ago, I thought it was cool to name a variable that represents a Fuel Action Low Sequence Enzyme as FALSE. It is so easy to remember! Yet so confusing for the others or even for you after a while. The bottom line is: Never use this kind of abbreviations
- Names with similar meanings. To have fileIndex and fileNumber in the same place is a bad idea. There is no logical way to tell which is which.
- Different meanings, similar names. clientReps and clientRecs are hard to distinguish. They will probably create lots of confusion. Try to abbreviate differently or use synonyms.
- Similar-sounding names. Goal_Donor and Gold_Owner are tricky. It is hard to distinguish them in speech, so consider changing them.
- Numerals in names. There are few times when file1 and file2 are the most appropriate names. Almost in any case, you can do better than that.
- Names that are easily misspelled.I find myself typing cleint instead of client very often. This may not be a problem in a strong typed language used with a IDE that has auto-complete features, but it is a big problem in a weakly typed one. Because you never know in what program language you will program in 10 years, it is better to build healthy habits. Most English handbooks contain lists of commonly misspelled words.
- Do not differentiate only by letter capitalization. Even if most programming languages support this, it is probably a bad idea to do so. The “psychological” difference is too small.
- Avoid multiple languages. Decide on the natural language your program is written in. It is common to find names in many languages in multinational programs. And this is a big problem.
- Avoid using keyword names. Even if it sounds obvious, sometimes it happens. I had problems with SQL statements that used field names which where reserved keywords multiple times.
- Unrelated names. It is fun to see variable names like little_johnny, pookie, lex_luther. But this guarantees that nobody will understand what you mean. So express your humor in other ways.
About Routines
October 15, 2006
*By routine, I understand a function, method or a procedure.
Programming always comes down to writing routines. So doing it better might help
. Here are some steps that may improve their quality
- Start from a good design. Know in advance what routines your program needs and be sure that every routine does one thing. A routine that initialises the form, calculates default values and displays a welcome message is really bad.
- Give the routine a good name. A good name describes specifically what a routine does, For a function, the name should represent the returned value(e.g. getFont, sin). The name of a procedure should be a verb followed by an object that clearly defines what the procedure does. PrintReport, LogError, SaveCustomerData are good routine names. HandleStuff, SaveData, InitializeData are bad names. If you cannot find a good name for your routine, there is a problem with your design. Either the routine does more than one logical action, either less than one, either you understanding of the problem is wrong. Bad names=Bad Code.
- Start by writing pseudo-code. Pseudo-code is written in plain English. Pseudo-code should be clearly understood by someone unfamiliar with the specific programming language you use. After you are finished, revise it and write it in more detail. Repeat until it is harder to revise the pseudo-code than writing the code itself.
- Comment your pseudo-code, and after each line write the correspondent line of program code. Programming should be an almost mechanical action by now. The technique of writing pseudo-code helps by unleashing you from the programming language details and by providing the code comments even before writing the code
- Compile only at the end. Challenge yourself to write code that compiles. Compiling often distracts your attention and tempts you into using hacks just to see your code compiled.
- No hacking! Using hacks is bad practice. Think of the right solution. A hack has the chance to remain there even if you say you will change it ASAP. Hacks pile up and soon the project gets out of control. It is better to go slower than hack your way through