Archive | Programming RSS feed for this section

Warning “headers already sent by” while developing PHP webapp

22 May

What are headers?

Every time you respond a request, there are some information attached at the beginning of the response. Those are headers. For example:

HTTP/1.1 200 OK
Date: Wed, 08 Feb 2006 08:20:07 GMT
Server: Apache/2.0.54 (Debian GNU/Linux) mod_auth_pgsql/2.0.2b1 mod_ssl/2.0.54 OpenSSL/0.9.7e
X-Powered-By: PHP/4.4.0
Transfer-Encoding: chunked
Content-Type: text/html

Those headers is used to control the browser behaviors such as caching, rendering… If you want to learn more about how to use them, you can see more helps and instructions from here:

http://www.expertsrt.com/tutorials/Matt/HTTP_headers.html

Why do I get this warning?

Sometimes, you receive the warnings like this: “… headers already sent by”. It’s also mentioned in that article, but I will summarize the reason here. You got that warning because you call a function like header() or session_start() after sending page content. The page content includes what you sent to browser by echo, print, or whatever outside the server tag <? … ?>. Be caution: the headers must be sent before any other responses.

But actually I did not send any page content before calling header function? Why the warnings are still there?

This is one of the stupid bugs that PHP developers usually get. The page content includes ANY THINGS outside the server tags <? … ?>, that means white spaces and new line characters are also counted as well. Check your code again, maybe you left some blanks after the ending tag ?> in one of your included file.

Ah, one more thing! When you save your files as “UTF-8 with signature” on Windows platforms, 3 bytes with hex code “EFBBBF” will be inserted automatically at the beginning of the files. Those bytes are used to let Windows regconize that the file contains UTF-8 text. They’re unneeded in PHP because PHP won’t treat this signature. It considers those bytes as page content because they appear before the opening tag <?. As the result, any calling to header() and session_start() will be failed. If you’re using Zend Studio, choose the Encoding to “UTF-8 without signature” when saving.

Permission denied when using MSMQ

8 Apr

You want to develop an application using MSMQ service, but whenever you send or receive message from MSMQ, you receive a Permission Denied exception. A possible reason is that you may have installed MSMQ in workgroup mode.

You can find the resolution for this problem here: http://support.microsoft.com/kb/247394/EN-US/

To use queued components in Workgroup mode, you must do two things:

  • To allow for unsigned MSMQ messages, you must set the Authentication Level of the COM+ Application to None.
  • To access the queued components from a remote computer, you must specify a queue: moniker that contains the complete FormatName of the destination queue.

To change the Authentication Level of the COM+ Application that hosts the queued components, perform the following steps:

  1. Under Administrative Tools, open the Component Services MMC snap-in.
  2. Expand My Computer and browse to the COM+ Application that hosts the queued components.
  3. To open the property sheet for the application, right-click the application and select Properties. Click the Security tab, and select None for Authentication level for calls.

Migrating UTF8 data from MySQL 5.0 to 4.x (with latin charset)

27 Sep

Some of old PHP application use ANSI SQL to create table and insert data. So when I use those application with UTF8 content in MySQL 5.0, the default character-set of those table is latin1. The problems come when I want to move data to an older MySQL, the encoded content goes wrong.

So this is the solution:

mysqldump -u username –opt –default-character-set=latin1 –skip-set-charset –skip-tz-utc –skip-comments –single-transaction %1 %2 %3 %4 %5

This post will be updated later with full description.

Paging in SQL Server 2000

15 Jul

Oh man, working with M$ is hell. Yeah, they do very big things but they just forgot small but essential things.

M$ maybe the most stupid when not implementing paging functionality in T-SQL. Most of other DBMSs do that. Paging from server is a vital point, especially in web development. If you just do paging from client-side, you are wasting most of network traffics for unused records.

After searching thouroughly on Internet, I finally found out some ways to get arround this flaw. One uses dynamic SQL query to refine the query and add some ORDER BY + TOP clauses to get the result. Another way is to use a temporary table with an identity column used as a row number. I prefer the 2nd way. At least, it’s more secured.

alter procedure GetAuthors
(
@orderby 	nvarchar(100),
@recordsPerPage int,
@pageNumber 	int
)
AS
--  the amount of rows actually needed
declare @rowsNeeded int
--  the position where we need to start retrieving records
declare @startRecord int
-- initialize @rowsNeeded
set @rowsNeeded = (@pageNumber * @recordsPerPage)
-- initalize @startRecord
set @startRecord = ((@pageNumber-1) * @recordsPerPage) + 1

create table #temptable
(
 row		 int identity(1,1),
 au_id 		 varchar(20),
 au_lname	 varchar(40),
 au_fname	 varchar(20),
 phone	 	 char(12),
 address 	 varchar(40),
 city		 varchar(20),
 state	  	 char(2),
 zip		 char(5),
 contract	 bit
) 

-- set the rowcount to only the rows we need.
set rowcount @rowsNeeded

-- select data into a temp table,  I choose to
-- select all data, not just the key into the
-- temp table to avoid another join if the table is large.
insert into #temptable
(au_id,au_lname,au_fname,phone,address,city,state,zip,contract)
select au_id,
       au_lname,
       au_fname,
       phone,
       address,
       city,
       state,
       zip,
       contract
FROM authors
ORDER BY
CASE
WHEN @orderby=’au_lname’ THEN au_lname
WHEN @orderby=’au_fname’ THEN au_fname
WHEN @orderby=’au_id’    THEN au_id
END 
ASC
select row,
       au_id,
       au_lname,
       au_fname,
       phone,
       address,
       city,
       state,
       zip,
       contract
from #temptable
where row >=@startRecord

drop table #temptable

If you use SQL Server 2005, the life must be easier. You can use ROW_NUMBER to do paging:

WITH OrderedOrders AS
(select SalesOrderID, OrderDate,
Row_Number() OVER (order by OrderDate)as RN
from SalesOrderHeader )
SELECT * from OrderedOrders WHERE RN between 50 and 60

Well… At least, M$ have done something to improve the situation, but not all. It takes me all night to find out the way. I have even thought about switching to MySQL with a very simplier paging syntax, but this is my thesis :P, so I can’t.Thanks to the communities.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=31741

Okey, time to continue my work.

What is a framework?

25 Jun

Today I found an interesting article about design and strategy in programming. It provides a quite basic understanding about how to write a good application.

 http://www.codeproject.com/gen/design/WhatIsAFramework.asp

Those are quotes from the authors of Design Patterns that the author like, and I do so:

"When you use a toolkit, you write the main body of the application and call the code you want to reuse.  When you use a framework, you reuse the main body and write the code it calls."

"Not only can you build applications faster as a result, but the applications have similar structures.  They are easier to maintain, and they seem more consistent o their users.  On the other hand, you lose some creative freedom, since many design decisions have been made for you."

"If applications are hard to design, and toolkits are harder, then frameworks are hardest of all.  …Any substantive change to the framework's design would reduce its benefits considerably, since the framework's main contribution to an application is the architecture it defines.  Therefore it's imperative to design the framework to be as flexible and extensible as possible."