2009/03/30

Links is forced to open in a new window

All HTML links with target="_blank" usually means open in a new window. Some modern browser can open them in a new tab, which is one of enhancements that make Firefox famous.

But recently, the links of the page are forced to open in a new window other tan a new tab in my Firefox browser, although I have set it to open in a new tab in the Tools->option-tab.

I tried to Goolge an answer, but found nothing. Finally, I found the reason.

Try enter ‘about:config’ on your Firefox browser address bar, you will see a long list, type in ‘link’ in the Filter, you will see, below two parameters,

browser.link.open_newwindow and browser.link.open_newwindow.restriction

The default value of these two parameters are 3 and 2. But they were 1 and 1 on my browser.

browser.link.open.newwindow = 1 means open All HTML links with target="_blank" in a new tab, but it only work when browser.link.open_new.restriction = 2.

2009/03/24

iconv_substr() gives "Unknown error"

If Unknown error(0), then most likely it is this issue, iconv_substr() gives "Unknown error" when string length = 1" and it was fixed in PHP5.2, if you are still using PHP5.1, here is a workaround for this bug,

/*Reproduce code:
---------------*/
$s = 'x';
$s = iconv_substr($s, 0, 1, 'UTF-8');
var_dump($s);
/*Expected result:
----------------*/
string(1) "x"
/*Actual result:
--------------*/
bool(false)
//and Notice: iconv_substr() [function.iconv-substr.html]: Unknown error
//(0) in ...
/*A workaround:
-------------*/
$s = 'x';
$s .=" "; //add one more empty character
$s = iconv_substr($s, 0, 1, 'UTF-8');

But if you got Unknown error(12), which is what I got in lucene search engine. Then it is a issue in this thread, but don’t know the solution yet.

2009/03/20

Some https conf for sercurity

#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
#
ServerSignature Off
#Fri Mar 20 09:19:57 EDT 2009: per OReilly
ServerTokens ProductOnly

#
# Timeout: The number of seconds before receives and sends time out.
#
#Fri Mar 20 09:12:45 EDT 2009: Timeout 180  (reduced  per O'Reilly recommendation for DOS protection)
Timeout 60

Sessioin stuff

Session cookie lifetime in our server is 864000 sec, so it is 10days not 30minutes as we always assume.

When people logon online account,

  • 1. get billing/shipping info in account_billing/account_shipping table.
  • 2. then check if there is billing/shipping info in the billing/shipping table with the same session_id.
  • 3. if yes in step2, then update it with the one got from step1; if not, then insert with one got from step one if found in the step 1

2009/03/19

Trademark, Copyright, and Register symbols

Trademark, copyright, and register symbols in iso-8895-1 character set cannot be displayed correctly in other character set, for example, urf-8, or even iso-8895-15.

Try this function, which should fix this issue.

function tcr_encode($str) {
   $str = preg_replace("/".chr(153)."/",'™',$str); // trademark
   $str = preg_replace("/".chr(174)."/",'®',$str); // registered tratemark
   $str = preg_replace("/".chr(169)."/",'©',$str); // copyright
   return $str;
}
//change trademark, copyright, and register symbols to html entity
$title = tcr_encode($title);

2009/03/17

How to have variable in jquery selector

Seems very simple, but sometimes I just forget it.

var myid = "result";

$("#" + myid).hide(); work

$('"#' + myid + '"').hide(); doesn't work

2009/03/16

Cleaning up/Formatting user input

We use below function to clean up a user input:

function x_site_safe($input,$do_quot=true,$remv_quot=false) {
   $input = preg_replace("/</","",$input);
   $input = preg_replace("/>/","",$input);
   $input = preg_replace("/\(/","",$input);
   $input = preg_replace("/\)/","",$input);
   if ($do_quot)   $input = preg_replace("/\"/","&quot;",$input);
   if ($remv_quot) $input = preg_replace("/\"/","",$input);
   return $input;
}

Usage:
x_site_safe($input) : remove >, <, ( , ), and change " to &quot
x_site_safe($input, false): remove >, <, ( , and )

For example: (combine with stripslashes and trim)

$str = (isset($_GET['v'])) ? stripslashes(trim(x_site_safe($_GET['v'],false))) : "";

A little better practice on web development

Every time working on a new project, I do it on the development box, after getting code done and tested, I load new code to the production. It sounds simple, isn’t it? But sometime it is not that simple, for the environment between production and development is different, so I cannot just simple overwrite the production one when load the new code in, also, sometime I directly changed the code on the production for a small change without updating the code on the development. (not a good practice though, but for the small company’s website, which may a quick way.)

So I think I need some good practices to follow when I start a new project. Here they are,

1. Before change any single file, compare it with the production one. If see any new in the production (usually a very small change), update it back to the development one. Make sure it works on development.

2. Make a copy of the file you are going to work with. Add it into the project’s file list.

3. Work on the files on the development box and test.

4. When code ready, compare each file on the project’s file list, and double check the different between new file and backup file, and make sure any additions also available on the production. 

5. Before loading the new code into production. Make a copy of the code on the production as filename.OLD. Make necessary change on the development code to meet the production’s environment and save as filename.NEW.

6. OK final step, check the order of files need to change, for a big project, the code updated may be done after 9:00pm. Basically it is copy filename.NEW to filename.

In any failures above, just copy the filename.OLD back to filename.

2009/03/15

Error: Permission denied to call method Location.toString

I got this error in both Firebug and Firefox add-on Web Developer . "Error: Permission denied to call method Location.toString", when visiting my own website.

I checked carefully of the webpage, but did not find any problem of my code. Finally, I found the problem was from a Flash Ad. from Google AdSense.

Anyway, I need to keep Ad.

2009/03/13

IE Security Warning

Most of time I am using Firefox, so I just found that the checkout page of my company's website has a problem in IE today. We bought the SSL certificate, so it is start with 'https.' However, when the page loaded in IE, it prompts,

This page contains both secure and nonsecure items. ...

secure

For user, the solution is that changing IE securtiy setting to display mixed content. Internet Options -> Security -> Custom level -> Enable Dispaly mixed content (in the middle of settings box, took me some time to find it :-))

By default, IE prompts when it see a mixed content page, so for developer, solution is to fix it other than telling user how to change the security settings. How? The reason of this warning is that in the page has both 'https" and "http" links, for example, some images links using 'http'. So changing those "http" links to "https" will fix it, at least in my case.

Updated: (3/16/09)

Actually it depends on how you registered the SSL domain, if it is www.yourdomain.com, then no matter you use http or https. if it is secure.yourdomain.com, then you should correct www.youdomain.com to secure.yourdomain.com.

2009/03/09

XML Parsing Error: Junk After Document Element

I got this error, XML Parsing Error: Junk After Document Element. My XMl was like this,

<?xml version="1.0" encoding="iso-8859-1" ?>
<billing_id>1234</billing_id>
<f_name>adam</f_name>

After some searching, I knew why,

A well-formed XML document must have one, and only one, top-level element. Anything after that element's end-tag will be ignored at best, and possibly reported as an error such as the one you're getting here.

So I fixed the problem by adding a top-level element <rsp>

<?xml version="1.0" encoding="iso-8859-1" ?>
<rsp>
  <billing_id>1234</billing_id>
  <f_name>adam</f_name>
</rsp>

2009/03/06

AdSense shows a blank space

I created an AdSense account a few days ago. But when I integrated it into my website, it only showed a blank space where I put the code without any contents. I googled the web and did not find a solution. Looks like not too many people have this problem, in case you get the same problem, here are some possible reason and solutions.

Reason 1: When you create an Ad unit, you choose to show a solid color when AdSense cannot find a match content on the page. And the solid color you picked is the same as the background color on the problem area.  
Solution 1: Most of time this is not a case. If it does, then try to used a different solid color.

Reason 2: You just created/updated an Ad unit, which usually take up to 10 minute to load
Solution 2: Wait 10 minutes to see it works or not

Reason 3: The AdSense code does not correctly paste. You cannot write the code in a single line.
Solution 3:

The is happen to me, see the code below,

//a sinlge line below causes a problem
<script type="text/javascript"><!-- google_ad_client = "pub-xxx"; /* 160x600, created 3/4/09 */ google_ad_slot = "9031182928"; google_ad_width = 160; google_ad_height = 600; //--> </script> 

After changed it like below, it fixed the problem.

<script type="text/javascript"><!-- 
google_ad_client = "pub-xxx";
 /* 160x600, created 3/4/09 */
 google_ad_slot = "9031182928";
 google_ad_width = 160; 
 google_ad_height = 600;
 //--> 
 </script>

2009/03/05

Mark all gmails as read

I use Outlook to revieve emails a lot. Sometimes, especially I'm at work, I also logon my Gmail thru Firefox. I installed the Google toolbar for firefox with Gmail notifier to let me know when a new email coming in.
Here is a problem, using Outlook to recieve emails, the received emails in you account are still shown as unread. So Gmail notifier keep on indicating there are new emails when I restart the firefox, but acutally, they are not really new. So I decided to mark those emails as read, however, it's painful if you have tons of emails unread. 
Google is always my best friend, I found a solution at http://www.woodwardweb.com/technology/marking_all_mai.html   
To mark all mail as read:-
   1. In the search box, type "is:unread"
   2. In the select area press "All"
   3. A link will appear that says "Select all conversations that match this search", press it
   4. From the drop down, select "Mark All as Read".

2009/03/03

Apache built-in funtion, server-status

There is mod_status built into Apache web server to get server status from web browser. With this module you can easily find out how well your server is performing.
You can turn in on in httpd.conf file, like below,
<Location /server-status>
    SetHandler server-status
    AuthType Basic
    AuthName "Admin Area"
    AuthUserFile /var/www/passwd/passwords
    Require user admin
    Order deny,allow
    Deny from all
    Allow from 172.16
    Satisfy any
</Location>
All reports are generated in a html format. You can easily find out following type of information:
   1. Total number of worker serving requests
   2. Total number of of idle worker
   3. Find the status of each worker, the number of requests that worker has performed and the total number of bytes served by the worker
   4. Total number byte count served
   5. Other information such as CPU usage, number of requests per second,current hosts and requests being processed etc.
For example, you may see 1&2 like below,
233 requests currently being processed, 13 idle workers
Here, 233 is simultaneously running requests, which cannot access the maximum number you set up. Depend on how busy your server is, you may make this number bigger. The default number is 256.
Usually, you can set this in your http.conf file, like below,
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      512
MaxClients       512
MaxRequestsPerChild  4000
</IfModule>