How To Make Cookies In 5 Minutes?

Tri Nguyen | July 22, 2011 | ADD COMMENT

What is cookie?

Cookie, also known as HTTP Cookie, Web Cookie, and Browser Cookie, is often used to send state information to the browsers and for browsers to return state of information back to server. Cookie is not a software and cannot be programmed. It's simply a plain text file that stores user activity, preferences, session data, and more.

Creating Cookie

Creating a cookie means create a text file with specific data on the user machine. A good example would be the the "Like" feature below. When user click on the "Like" button, it inserts a vote for this blog entry as well as creating a cookie on the user machine that will expired in 24 hours. On Page Load, if the cookie exist, it hides the vote option and/or denied multiple vote within 24 hours, if user somehow again access to the vote function.

Example

This cookie basically does nothing except storing the date and time of user last visit.

//Create new Http Cookie
HttpCookie my_cookie = new HttpCookie("cookie_name");

my_cookie["last_visit"] = DateTime.Now.ToLongDateString();

my_cookie.Expires = DateTime.Now.AddDays(30); //expired in 30 days

Response.Cookies.Add(my_cookie); //Add cookie to user machine

Deleting Cookie

The only way to remove a cookie is to delete manually, assuming you know where your browser stores cookies, or replacing with an expired cookie. There are couple ways to carry out this task in ASP.NET:

Example - Delete By Replacing With An Expired Cookie
//Request cookie from user machine
HttpCookie my_cookie = Request.Cookies["cookie_name"];

//If cookie exist on user machine, then replace with an expired cookie
if (my_cookie)
{
    //Create a new cookie with the same name
    my_cookie = new HttpCookie("cookie_name");

    //Set expiration to the past hour to make it expired
    my_cookie.Expires = DateTime.Now.AddHours(-1);

    //Add to user machine, which will replace the existing one since they have the same name
    Response.Cookies.Add(my_cookie);
}
C# Example - Delete By Calling ASP.NET Built-in Function

This is a single line function call but the code behind really does the same as above.

Response.Cookies.Remove("cookie_name");

Securing Files In ASP.NET, Preventing Direct Access From Browsers

Tri Nguyen | June 20, 2011 | ADD COMMENT

Preventing Direct File Access

Assuming any file in the "secure" folder, http://www.website.com/files/secure/, is protected and may only be accessed by authorized user. This mean, if we place a "test.pdf" file inside the secure folder, it cannot be accessed when user types "http://www.website.com/files/secure/test.pdf" in the browser. To achieve that, we need to add the following handler in the Web.Config file.

<system.web>
   <httpHandlers>
      <add verb="*" path="files/secure/*.*" type="System.Web.HttpForbiddenHandler"/>
   </httpHandlers>
</system.web>

Delivering Files To Authorized Users

The handler above instructs ASP.NET to return 403 Error when user attempts to access any file inside the "secure" folder. Therefore, in order to grant user access to the secured files, we need a dynamic page that will read the file content and flush out into the response stream, e.g. http://www.website.com/file.aspx?id=12345, where 12345 is the identification for "test.pdf" file. There a number of things that we can check in file.aspx page before granting access to users such as login, expiration date, etc. The following is a quick demo written in VB.NET:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   Me.Response.Clear()

   Dim strFileId as String = Me.Request.QueryString("id").ToString()

   If(strFileId.length > 0) Then
     
      If (System.IO.File.Exists(Server.MapPath("files/secure/test.pdf")) Then

         Me.Response.ContentType = "application/x-pdf"

         Dim arrBytes As Byte() = System.IO.File.ReadAllBytes(Server.MapPath("files/secure/test.pdf"))

         If (arrBytes IsNot Nothing And arrBytes.Length > 0) Then

            Me.Response.OutputStream.Write(arrBytes, 0, arrBytes.Length)

         Else

            Me.Response.Write("File Not Found")

         End If

      Else

         Me.Response.Write("File Not Found")

      End If

   Else

      Me.Response.Write("File Not Found")

   End If

   Me.Response.End()
  
End Sub

If anyone has a different idea of handling this kind of data processing, I would love to hear them. Once again, thank you for visiting.

Sending Text Message From Your E-mail

Tri Nguyen | June 13, 2011 | ADD COMMENT

Generally, Text Messaging System is very much similar to any other E-mail Systems, in term structures and data processes. In fact, I think it's legitimate to consider it as a modified version of an E-mail system. Perhaps, that is the reason why our mobile number also comes in a form of E-mail, which not many people are aware about the fact that our mobile number is really a disguised version of an E-mail.

The following is a list of some of the major carriers in the United States and their mobile E-mail addresses. If your carrier is not listed, try to Google it. You'll be surprise to see more information than you've intended to find.

Carrier Email
T-Mobile [10-digit phone number]@tmomail.net
AT&T [10-digit phone number]@txt.att.net
Sprint [10-digit phone number]@messaging.sprintpcs.com
Verizon [10-digit phone number]@vtext.com

I also implemented a simple mail form to demonstrate this process. See Our Demo

Share on Facebook - Two Simple Methods

Tri Nguyen | June 08, 2011 | ADD COMMENT
Method 1:
http://www.facebook.com/sharer.php?u=http://www.tringuyen.info

In the example above, all we have to do is specifying the link that we want to share. Facebook API will pick up the default page title, description, and image automatically. If default image is not specified in the header, then first image will be used.
View Demo 1

Method 2:

http://www.facebook.com/sharer.php?s=100&p[title]=TRINGUYEN.INFO%20-%20A%20Developer%20Journal&p[url]=http://tringuyen.info&p[summary]=Click%20Here%20to%20visit%20TriNGUYEN.INFO%20-%20A%20developer%20journal&p[images][0]=http://tringuyen.info/images/fb_icon.jpg

The second example allows us to fully customize all 4 properties: page title, share link, summary, image. This example is useful when adding share links to a list of entries such as blog entries or news items. If we use the first example above to share a list of items, then all share entries will look the same, which basically will make the shared content look boring and ineffective.
View Demo 2

Free Phone Call To The United States and Canada Using Gmail Accounts

Tri Nguyen | June 05, 2011 | ADD COMMENT

If you don't see the "Call phone" option after logging into your Gmail account, that mean you have not signed up for Google Voice account. In such case, there is no need to worry, you can easily sign up at http://www.gmail.com/call/.

I have been using this feature for quite sometime now, and I really enjoy it. It's very convenience, especially when my cell phone had weak signal or when I forgot to bring my phone. More importantly, it allows love ones outside the United States to make unlimited phone call to United States and Canada for FREE!