View Full Version : need help with a complex snippet for chat room

05-05-2009, 05:17 PM
Hi there,

I'm fairly new to php but have managed in the past three weeks to go from zero knowledge of php/mysql to building a fully functioning chat room with lots of features.

One thing I have not been able to figure out though.

First, I'll describe how my chat works. It's a php script, and once a user is logged in, they are able to chat, etc. It is a post-pull system. Meaning the user must hit "enter" to poll the chat site. In order to help make this system a little faster, I installed Zend Optimizer/Accelerator with my php build so the scripts will be a bit faster with multiple users polling the server for that specific script. I prefer this method at the moment (while learning) over the live update type of chats with java, etc.

So, user is logged in and can chat via a text input field named "mes" (EX <input type=text name=mes maxlength=600>)

I want to implement a /command system. I've tried everything I can find snippet wise and code wise on the internet and google and forums, but I haven't been succesful in doing what I want to do, which is this basically

User types /command [parameter] etc etc , Example /kick USERNAME REASON

once submitted, I need an if statement that recognizes if the first string (in this case /kick ) begins with a forward slash /
If it does begin with a forward slash, I then need it to strip the forward slash from that word (ive actually succesfully made a preg_replace that does this part but cant integrate it overall) then, once the forward slash has been stripped and replaced with "nothing", I want to check if (!file_exists(/path/to/command/files/command.php) then display error, else, include command.php

I'm going to put all of the user commands into their own PHP files in a directory to cut out on the main chat script having an unneccessary extra 5,000 lines of code (there will be upwards of 30-40 commands)

this way, if a forward slash / exists at the beginning of the first word in the input field, it is stripped, and a check is done to see if the file exists, if it does exist, use the php INCLUDE command so the code for that command is executed in the main chat script execution, if not, return an error saying * Sorry that command is not recognized or does not exist.

any help would be greatly , greatly appreciated from a junior php learner in progress.

05-05-2009, 06:47 PM
That sounds like you already put a lot of thought into it. What exactly are you having trouble with?