Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP/SQL Server Based Chat program

0.00/5 (No votes)
12 Nov 2003 5  
Simple ASP/SQL Server Chat application

A sample chat window

Introduction

This chat program was created simply because I didn�t want to buy one that had everything I wanted. This application is certainly not a complete application but it does take care of most general chat functions. I even included a few things some other applications don�t have such as smileys, language filters and room invitations.

Background

Since I have a content management system that uses a domain_id identifier, I wanted to be able to offer the chat program to many of my clients without having to create a database for each one of them. So I added the domain_id identifier to allow many domains to use it without cross contamination. You will see that almost all functions will filter by this identifier.

The database is SQL Server. I tried to use an Access database but I had trouble getting Access to support multiple recordsets in a singe SQL call. Instead of describing the database in detail, I have included a script that will build the SQL database. The data_access.asp contains the username and password that is written into the SQL script. These can be changed as long as they are the same in both places. I use a separate config file when I integrate these. For the demo, I simply hard coded them.

Using the code

Install

The code simply needs to be copied to any ASP virtual directory. No global.asa is needed. A database script is provided and just simply needs to be ran on the SQL Server with create database privileges.

Configure

In the install folder include/data_access.asp, simply replace the settings with your server info and you are all set. In my applications, this is an application variable. You may handle it however you wish.

Run

default.htm is a sample of how you can integrate the chat into any page. Sessions are used to track user info and stuff like that but by changing the domain_id parameter, you can run as many simultaneous instances as you want.

Happy chatting!

Database

  1. chat_rooms
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [room_name] [varchar] (150),
    [system_room] [int] NULL ,
    [private] [int] NULL ,
    [private_owner] [int] NULL ,
    [last_login] [datetime] NULL ,
    [domain_id] [int] NULL
  2. chat_users
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [user_name] [varchar] (50,
    [room_id] [int] NULL ,
    [date_in] [datetime] NULL ,
    [date_out] [datetime] NULL ,
    [user_ip] [varchar] (50),
    [active_time] [datetime] NULL ,
    [room_time] [datetime] NULL ,
    [domain_id] [int] NULL
  3. chat_user_invite
    [user_name] [varchar] (150),
    [room_id] [int] NULL ,
    [room_name] [varchar] (150),
    [private_room] [int] NULL ,
    [from_user] [varchar] (150),
    [domain_id] [int] NULL
  4. chat_msg
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [room_id] [int] NOT NULL ,
    [msg_ln] [varchar] (8000),
    [date_posted] [datetime] NULL ,
    [sortby] [int] NULL ,
    [sysmsg] [int] NULL
  5. chat_config
    [domain_id] [int] NULL ,
    [adm_uid] [varchar] (50)

This is the list of ASP files in the application

  1. admin_menu.asp

    Admin Menu is the menu screen for the administrator to use. It is basically a lobby and shows all the rooms that are in the system regardless if private. This screen has several options.

    1. Create a new room or system room. A system room is persistent and stays active until it is actually removed. A regular room stays until the last person logs out of it.
    2. Destroy rooms. If a room is removed with people in it, they are sent back to the lobby.
    3. Change the administrator user name.
    4. Link to another screen where users are listed. (users_view.asp)
    5. Link to another screen to set session options. (chat_user_options.asp)
  2. admin_view.asp

    Admin View is the chat window for the administrator. It is the same, but the language filter is turned off and the logging is turned off. It still uses the buttons.asp and the banner.asp.

  3. banner.asp

    Banner is used to display the room name, logout and return to lobby buttons.

  4. buttons.asp

    Buttons is the window used to type the message. It contains a content-editable DIV tag and a button. Some JavaScript is used to control the formatting buttons as well as some filtering for the query string.

  5. chat_user_options.asp

    This window is basically an HTML form to gather and display the session options for the user. I don�t bother storing these options in the database since I don�t store any user profiles. This could be altered and significantly enhanced.

  6. chatroom.asp

    This is the frameset for the banner, chat window and buttons. It contains a couple of script functions to check for session state and admin status.

  7. chatwindow.asp

    This is the actual message history window. It does nothing more than display the messages in formatted text as well as the actual smiley replacements and the language filters. It also checks for room validity in case it was deleted while you were in there.

  8. createroom.asp

    This is the backend file that actually does the database insert and validation of the room. It uses an array to construct the invite list. If a room already exists, it automatically appends a number to the end of the room. I could have confirmed with the user to accept this new room name but I opted to just create it.

  9. inframe.asp

    This file is the same as default.asp. It can be an include into a formatted page.

  10. invite_inc.asp

    This is an include file that simply runs the check to see if a user was invited into a room. It handles the response and redirects based on the invitation answer.

  11. lobby.asp

    The lobby is the first place you see when you create your login. It lists all the rooms and allows you to create a new room. (newroom.asp)

  12. login.asp

    This is simply the login page.

  13. loginuser.asp

    This is the backend code page to validate a login, or suggest a new login. It does all the admin validation and sets session variables once authenticated. This can be enhanced to include SQL validation or even Active directory validation.

  14. newroom.asp

    This displays the HTML form to gather the room name, and it allows users to type in other screen names to invite.

  15. toggleroom.asp

    This is a simple file to set a session variable to indicate a room was opened in the lobby, to view the users that are logged in.

  16. updateuseroptions.asp

    This is the backend code file to save the user options into the session. It redirects to chatroom.asp or lobby.asp.

  17. users_view.asp

    This admin screen shows all users logged in and gives the option to drop that user.

  18. updateadminoption.asp

    This file simply updates the database to change the admin username for the domain.

  19. mod_main.asp

    This is a global include in every ASP page. It contains the logintoroom and logoutofroom functions. It is where I put all superfluous functions. It also includes the data_access.asp which is the global data connection module.

  20. data_access.asp

    This is a module that I use in all my applications. It contains all the necessary functions to connect easily to a database using ADO. I use it in VB, ASP and even Access.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here