2009/06/30

$_SERVER to get current URL

Checked PHP Manual,

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

If you are using apache, check this page,

SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html
SCRIPT_FILENAME=/u/rse/.www/index.html
SCRIPT_URL=/u/rse/
SCRIPT_URI=http://en1.engelschall.com/u/rse/

In my server, I can use below code to get URL without query string (or ‘?q=’).

$url = getenv("SCRIPT_URI");
//or
$url = $_SERVER['SCRIPT_URI'];

2009/06/25

include library files

If you hosted your site somewhere, your probably wanted to know where the root directory path is, in order to include (or require) some library files, which requires a whole path.

If you hosted on linux system, then it is easy to find it out, by

$docRoot = getenv("DOCUMENT_ROOT");
include ($docRoot . "path/to/yourfile");

However, if you hosted on IIS system, then getenv would not work. Here is a workaround:

require_once(dirname(__FILE__) . '/../includes/utils.php');
require_once(dirname(__FILE__) . '/includes/utils.php');
Note: direname(__FILE__)return the current dir name, so you need to tweak it a bit (for example, ‘..’ means jump up a level) to find out a correct library files.

2009/06/24

escape single quote in javascript

Problem:

alias_term = trim(alias_term);
var str = '<a href="javascript:updt_mapping(\'alias\',\''+alias_term+'\');">Update Alias</a>';
Using above code, when the alias_term is something like k’nex, which has single quote, it would broke the javascript, for the javascript statement become updt_mapping(‘alias’, ‘k’net’)

Solution:

escape single quote, but you could not using escape function, alias_term = escape(alias_term), but replace function, alias_term = alias_term.replace(/\'/,"\\'");

2009/06/23

Some notes about yslow

Company’s webstie is slow. The boss wanted to know why and I am trying to find out why.

I think form web developer viewpoint, what you can do is optimize the web component. If your site hosted on a bad server, then you effort on optimizing the web component is minor. Anyway Firefox fox yslow seems to be a great tool helping you do the website optimization.

1. make fewer HTTP requests.

I think most of current sites have a same problem that a page contain too many external javascript scripts. yslow suggests to try combining them into one, however, most of time this is not a solution at all because you just could not combine them.

2. yslow suggest move all javascript at bottom.

If you should every javascritp snippets, which are relied to those javascripts at header, is running after document ready, then you may do this. Otherwise, you may break some of them.

3. Minify javascript and css

I do some of them. Just too lazy to do all of this, because you are going to update those minified files, that is pain. You have to edit the unminified version, test it and then minfied it again (unless you have a good eye to modify on a minified file)

4. Add expires headers and compress components with gzip

This can be done by updating apache setting …

5. Use a content delivery network

I think this is a big reason the site is slow, if you site hosted on a fast and stable content  delivery network, then you site is most likely running faster.

Note: Just found that yslow does not recognized old style JavaScript tags, like

<script language="JavaScript" src="/js/ajax.js"></script>
//yslow uses type to determine it is javascript or not
<script type="text/javascript" src="/js/ajax.js"></script>

2009/06/18

Remove all posts

Sometimes you may want to remove all posts from your Wordpress. You can do this from Wordpress dashboard, but you have to do delete them page by page, and each page has only 20 posts.

If you really wanted to remove all post at a time, here is Perl script to do so.

use WordPress::XMLRPC;
my $o = WordPress::XMLRPC->new({
username => 'admin',
password => 'yourpass',
proxy => '/path/to/your/xmlrpc.php',
});
#print $o->getPost();
my $post = $o->getRecentPosts(500); #500 meant get 500 posts
for $p (@$post) {
 $id =  $p->{'postid'};
 #print "$id\t";
 $o->deletePost($id);
}
PERL WORDPRESS XMLRPC Module can be found at here