存档

2010年1月 的存档

判断一个数是否是2的次方

2010年1月20日 1 条评论

经典的:
int IsPower(unsigned n)
{
if(n==0)
return 1;
while(n)
{
if(n%2==0)
{
n = n/2;
if(n==1)
return 1;
}
else return 0;
}
}
不必解释

超强的:
int IsPower(unsigned n)
{
return (n&&!(n&(n-1)));
}

解释:
如果一个数是2的次方,则转成2进制是首位为1,其余都为0,比如:
2(10) 4(100) 8(1000) 16(10000)……

如果一个数和全1的相与还是等于自己,则这个数就是2的次方

n&(n-1)计算的是全零的情况,故!(n&(n-1))是全1的情况

转自 http://hi.baidu.com/mzyse/blog/item/6b5f5517d5d9d30cc83d6da3.html

有关IIS下zencart的伪静态设置

2010年1月16日 没有评论

公司的zencart 项目放在了window2003的服务器上,为了seo的考虑,开启了搜索引擎优化模块,可是碰到了一个问题,url的伪静态是通过网站根目录下的.htaccess文件来实现的,这需要apache的rewrite模块,可是iis中怎么实现?其实可以通过iis的ISAPI_Rewrite扩展来实现,下载一个ISAPI Rewrite的安装包,Lite免费版本ISAPI_Rewrite Lite (freeware)即可。安装运行后,打开IIS,在网站单击右键选属性,就可以看到ISAPI_Rewrite已经被加入到了ISAPI筛选器中。在ISAPI_Rewrite的安装目录下,找到 httpd.ini 在此文件中输入Rewrite的规则即可,这里的规则基本跟apache的rewrite差不多,不过在我的测试过程中,发现了一些问题,例如http.ini中不能使用%{QUERY_STRING}这样的服务器变量,也不知道是不是我服务器配置的原因,这里我对 zencart 里的.htaccess文件需要经过一些修改解决了这个问题

RewriteRule ^(.*)-p-(.*).html$ index\.php?main_page=product_info&products_id=$2&%{QUERY_STRING} [L]

修改为,无非是一个简单的正则替换

RewriteRule ^(.*)-p-(.*).html(\?(.*))?$ index\.php?main_page=product_info&products_id=$2&$4 [L]

有关zen cart集成支付宝借口时提示ILLEGAL_SIGN 错误解决

2010年1月8日 6 条评论

这次开发zencart的过程中要用过国内大名鼎鼎的支付宝接口,从zencart.cn上下了jack的 zen cart 支付宝模块从后台安装一切ok,可是测试购物的时候遇到了 ILLEGAL_SIGN错误,找了很多答案,仔仔细细看了阿里提供的api文档,还是找不出问题所在。看来得靠自己了,对着firebug,一个个核对网站传输给支付宝的post数据,发现多了 btn_submit.x,btn_submit.x 这两个参数,我把form 的method改成get,从地址栏中去掉这两个参数再打开,成功转向支付宝收银台页面,看来果然是两个参数搞得鬼!
google了下submit.x,得到如下答案
根据图形提交按钮行为W3C的描述 :

When a pointing device is used to click on the image, the form is submitted and the click coordinates passed to the server.
当指针设备用于在图像上点击,表单提交和点击坐标传递给服务器。

The x value is measured in pixels from the left of the image, and the y value in pixels from the top of the image.
在x值的单位是像素从左边的形象,以像素为单位从图像的顶部y值。

The submitted data includes name.x=x-value and name.y=y-value where “name” is the value of the name attribute, and x-value and y-value are the x and y coordinate values, respectively.
提交的数据包括name.x = x值和name.y = y值在“名称”的名称属性值和x值和y值是x和y坐标值。

于是这还是W3C的标准,现在的问题就是如何避免浏览器“多此一举”了,很简单,在表单添加onsubmit=”this.submit();return false;”,即修改zencart的订单确认页面模板tpl_checkout_confirmation_default.php文件中的

echo zen_draw_form(‘checkout_confirmation’, $form_action_url, ‘post’, ‘id=”checkout_confirmation” onsubmit=”submitonce();”‘);

修改为

echo zen_draw_form(‘checkout_confirmation’, $form_action_url, ‘post’, ‘id=”checkout_confirmation” onsubmit=”this.submit();return false;”‘);