Monday, September 7, 2009

PHP Cookies: How to Set Cookies & Get Cookies

Cookies don’t have to be an essential part of a website but can provide some of the “little things” that can set your website apart from the rest. Cookies are small tidbits of information that you save on the client’s computer so that you can access them next time they visit the website. Session ID’s are also usually held in cookies.

So what are the most popular uses of cookies? They are:

  • To store username/password information so that the user doesn’t have to log in every time they visit the website (”remember me” sign ins).

  • To simply remember the user’s name.

  • To keep track of a user’s progress during a specified process.

  • To remember a user’s theme.


Easier way to manage your ASP.NET Cache

Was recently told by a client of mine that the ASP.NET app I developed was WAY TOO SLOW! I had to agree; the site was pinging the database twice on every load of the home page. So I said, "Give me a week... I'll make it work better". I went home feeling bad... my app was slow and I really didn't know where to begin. I had a lot of code that depended on pinging the database and I didn't want to sift through it all. What I ended up doing was using the cache object.

I started to look at other open source projects and their approaches to caching. Than DotNetKicks' cache manager caught my eye! I went off that idea and created my own way of implementing an object to interface with the cache object, making it more manageable.

The Cache manager I wrote has 3 methods (Grab, insert, clear), a constructor and 2 properties (CacheKey, CacheDuration). So here's my code in VB and C#:

Setting up a Subversion Server under Windows

A) Download SubversionYou'll need the latest version of..

B) Install Subversion

  1. Unzip the Windows binaries to a folder of your choice. I chose c:\program files\subversion\ as my path.

  2. Now, add the subversion binaries to the path environment variable for the machine. I used %programfiles%\subversion\bin\

  3. You'll also need another environment variable, SVN_EDITOR, set to the text editor of your choice. I used c:\windows\notepad.exe


C) Create a Repository

  1. Open a command prompt and type
    svnadmin create "c:\Documents and Settings\Subversion Repository"


  2. Navigate to the folder we just created. Within that folder, uncomment the following lines in the /conf/svnserve.conf file:
    [general]
    anon-access = read
    auth-access = write
    password-db = passwd

    Next, uncomment these lines in the /conf/passwd file:
    [users]
    harry = harryssecret
    sally = sallyssecret



D) Verify that everything is working

  1. Start the subversion server by issuing this command in the command window:
    svnserve --daemon --root "C:\Documents and Settings\Subversion Repository"


  2. Create a project by opening a second command window and entering this command:
    svn mkdir svn://localhost/myproject

    It's a standard Subversion convention to have three folders at the root of a project:
    /trunk
    /branches
    /tags


  3. At this point, Notepad should launch:Enter any comment you want at the top of the file, then save and exit.

  4. You'll now be prompted for credentials. In my case I was prompted for the administrator credentials as well:
    Authentication realm:  0f1a8b11-d50b-344d-9dc7-0d9ba12e22df
    Password for 'Administrator': *********
    Authentication realm: 0f1a8b11-d50b-344d-9dc7-0d9ba12e22df
    Username: sally
    Password for 'sally': ************

    Committed revision 1.

    Congratulations! You just checked a change into Subversion!


E) Start the server as a service

  1. Stop the existing command window that's running svnserve by pressing CTRL+C.

  2. Copy the file SVNService.exe from the zip file of the same name to the subversion\bin folder.

  3. Install the service by issuing the following commands:
    svnservice -install --daemon --root "C:\Documents and Settings\Subversion Repository"
    sc config svnservice start= auto
    net start svnservice


  4. Test the new service by listing all the files in the repository:
    svn ls svn://localhost/

    You should see the single project we created earlier, myproject/


F) Set up the shell extension

  1. Run the TortoiseSVN installer. It will tell you to restart, but you don't need to.

  2. Create a project folder somewhere on your hard drive. Right click in that folder and select "SVN Checkout..."type svn://localhost/myproject/ for the repository URL and click OK.


  3. Create a new file in that directory. Right click the file and select "TortoiseSVN, Add"

  4. The file hasn't actually been checked in yet. Subversion batches any changes and commits them as one atomic operation. To send all your changes to the server, right click and select "SVN Commit":


And we're done! You now have a networked Subversion server and client set up on your machine. Note that the default port for svnserve is 3690.

For more tips on using subversion, take a look at the free O'Reilly Subversion book.

Javascript: Print preview window

I would like to create a print preview window that will display only the main content of the page that a person would really want to print. It can work fine on IE and Firefox..You can test other browser. :D



Testing Print Preview



Print this stuff.

Print Preview

Don't forget create print.htm within blank content.

Thanks

CongNguyen

Encrypt/Decrypt string VB.Net

Imports System.Security.Cryptography

Encryption Coding Is:

/*
* [strText]: string is that you need to encrypt
*[strEncrKey]: is the key to decrypt the string that has encryption

*/

Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
Dim des As New DESCryptoServiceProvider
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
cs.Write(InputByteArray, 0, InputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function

Decryption Code Is:
/*
* [strText]: a string that has been encrypt with the above method
*[sDecrKey]: string is the key needed to decrypt
*/
Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function

Call Function:
Return Encrypt("string is that you need to encrypt", "abc123")
Return Decrypt("
string that has been encrypt with the above method
", "abc123")

Thursday, January 29, 2009

Attack a Team Morale Problem on Many Fronts

Morale problems don't happen overnight, and they cannot be resolved overnight. Typically, the complete causes and remedies are out of your control. However, as the project manager, there are some things that are within your control. Regardless of how much you can do, if the group sees you trying to help, they will feel better as well.

You need some feedback from the group to determine the cause of the morale problems. Once you understand the cause, there are usually multiple ways to help. Here are some examples.

  • Be a good listener. You will find that the simple act of listening will help people's morale. Being sympathetic and empathetic are key responses from the project manager. It shows that you at least recognize the problems and are concerned.

  • Say “thank you”. This is similar to being a good listener. If team members feel that the project manager recognizes their contributions, it will go a long way toward helping them feel better about their situation.

  • Assign more challenging work. This is a tough one because, in most cases, your work is your work and you cannot change the basic nature of that work. However, there are some things you can do to introduce new challenges. For instance, you can rotate people into new roles. If two people have done the same job for a long time, switch them. This gives each person an opportunity to learn new skills and new areas of expertise, while also giving you more backup coverage. You can also give people more responsibility. This might include letting new people manage the budget for the team, putting people in charge of subteams and assigning new people to manage the work of contractors.

  • Provide opportunities to learn new technologies. You can try to rotate people into new technologies, switch responsibilities to allow people to learn new skills, and increase the training opportunities.

  • Make sure people know what is expected of them. You should make sure people are clear on what their job responsibilities are, what their current work activities are, and how their contributions help the entire project to be successful.

  • Offer more flexibility. Allowing people more control over their jobs and lives can help morale. Examples of work flexibility include:

    • Offer flextime options to allow people to work early or late. This could also include four ten-hour days or allowing people to work early or late based on their personal preference.

    • Try to offer some form of telecommuting. Look at one to two days per week to start, perhaps just with selected trial people.

  • Get the right equipment. Make sure people have the right hardware and software they need to do their jobs. It is especially frustrating for people to work on slow equipment, especially when hardware is so cheap.

  • Look for opportunities to have fun. Look for an opportunity for social events, pizza parties, birthday cakes, etc.

  • Solicit opinions and ideas from employees. The project manager should encourage team members to become involved and offer their insights on assignments. If team members feel like their opinions and ideas are valuable, they will feel better about their situation.

There are many reasons for bad morale. Based on the reasons, there are also many ways to try to improve morale. The key is to recognize that the team will not perform as well if morale is bad. So, project managers should keep their eyes open for morale problems and look for ways to keep morale up. Regardless of the limitations of your role, there are always some things such as listening and saying “thanks” that are within your control. There may be many other responses in your control as well.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Wednesday, January 21, 2009

How to backup SVN repository

How to backup SVN repository

Nếu bạn thường hay sử dụng svn subversion để quản lý các project, thì việc backup định kỳ là rất cần thiết. Nếu lỡ ngày nào đó, svn server của bạn bị hỏng hoặc OS bị hư …. thì dữ liệu của bạn cũng có thể mất trắng, rơi vào hoàn cảnh như vậy thì bạn sẽ thấy tầm quan trọng của việc backup data là như thế nào. Có rất nhiều cách để backup repository.
  1. Cách đơn giản nhất là copy nguyên repository ra một nơi nào đó (usb, DVD, CD).
  2. Nếu bạn có dùng TortoiseSVN on window, thì bạn cũng có thể sử dụng chức năng export để backup, bằng cách chọn TortoiseSVN -> Export . Sau đó thì bạn chỉ định path để export
  3. Dùng command line.

Cách 1 chắc không có gì để nói thêm. Cách 2 thì cũng thủ công, nếu trong repository có 100 project, chẳng lẽ bạn backup từng project một, rất tốn thời gian. Bài này đề cập đến cách thứ 3, dùng command line để backup.

Cách thứ 3 là bạn thực hiện dump toàn bộ repository ra một file nào đó. Cách thực hiện như sau :

Open command line window

svnadmin dump [your repository location] > [destination file]

Ví dụ :

svnadmin dump /usr/local/svn_repos > /tmp/svn_backup.dump

Khi thực hiện lệnh trên thì bạn sẽ thấy các project dump ra như sau

* Dumped revision 0.

* Dumped revision 1.

* Dumped revision 2.

* Dumped revision 3.

…………………..

Bạn có thể dùng một editor nào đó để xem thông tin trong file .dump. Hoặc bạn có thể copy file này vào usb,CD,DVD để lưu giữ.

Sau này nếu bạn muốn restore file .dump đã backup thì bạn làm như sau :

  • Đầu tiên bạn tạo một repository
  • Sau đó dùng command svnadmin load

Cụ thể như sau :

Open command line window

svnadmin load [path of your repository] < [path of .dump file]

Ví dụ :

svnadmin dump /usr/svn_new_repos < /tmp/svn_backup.dump

Vậy là bạn thực hiện xong các bước backup và restore.

Ngoài ra, nếu bạn không muốn thực hiện các lệnh này mỗi khi backup, thì bạn có thể viết một đoạn script (.sh or .bat) hoặc PHP script đơn giản để thực hiện việc backup repository.

The end