Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2006, 11:59 AM   PM User | #1
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
Thumbs up Connecting to an IRC server with PHP

Many of you have probaly heard of, IRC (Internet Relay Chat). Developed in 1988 by Jarkko Oikarinen, it was a great way to communicate, it still is.

Today, i will show you how to create a simple irc client that will Recive from an IRC server. If intresent is shown in this tutorial i will make a second one on how to send text as well.

Lets Begin.
First we will need to create a file that contains the data for the IRC server (I.E. Serverhost, channel, port)

Config.php
PHP Code:
<?php
//The server host is the IP or DNS of the IRC server.
$server_host "irc.smirl.com";
//Server Port, this is the port that the irc server is running on. Deafult: 6667
$server_port 6667;
//Server Chanel, After connecting to the IRC server this is the channel it will join.
$server_chan "#smirl";
?>
Ok, now that thats out of the way, we are going to make the form that will allow the user to select a nickname to use on IRC, and connect and recive data from the irc server.

Index.php
PHP Code:
<?php
//First lets set the timeout limit to 0 so the page wont time out.
set_time_limit(0);
//Also inclue our config file
include("Config.php");
//Second lets grab our data from our form.
$nickname $_POST['nick'];
//Now lets check to see if there is a nickname set.
if(empty($nickname))
{
    
//Whoops we dont have a nickname set. 
    
echo "<form name=\"form1\" method=\"post\" action=\"index.php\">\n\r";
    echo 
"<p align=\"center\">Please Insert a Nickname.\n\r";
    echo 
"<input type=\"text\" name=\"nick\"> \n\r";
    echo 
"</p>\n\r";
    echo 
"<p align=\"center\">\n\r";
    echo 
"<input type=\"submit\" name=\"Submit\" value=\"Join IRC\">\n\r";
    echo 
"</p>\n\r";
    echo 
"</form>\n\r";
}
else
{
    
//Ok, We have a nickname, now lets connect.
    
$server = array(); //we will use an array to store all the server data.
    //Open the socket connection to the IRC server
    
$server['SOCKET'] = @fsockopen($server_host$server_port$errno$errstr2);
    if(
$server['SOCKET'])
    {
        
//Ok, we have connected to the server, now we have to send the login commands.
        
SendCommand("PASS NOPASS\n\r"); //Sends the password not needed for most servers
          
SendCommand("NICK $nickname\n\r"); //sends the nickname
          
SendCommand("USER $nickname USING PHP IRC\n\r"); //sends the user must have 4 paramters
        
while(!feof($server['SOCKET'])) //while we are connected to the server
        
{
            
$server['READ_BUFFER'] = fgets($server['SOCKET'], 1024); //get a line of data from the server
            
echo "[RECIVE] ".$server['READ_BUFFER']."<br>\n\r"//display the recived data from the server
            
            /*
            IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
            Or the client gets Disconnected
            */
            //Now lets check to see if we have joined the server
            
if(strpos($server['READ_BUFFER'], "422")) //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
            
{
                
//If we have joined the server
                
                
SendCommand("JOIN $server_chan\n\r"); //Join the chanel
            
}
            if(
substr($server['READ_BUFFER'], 06) == "PING :"//If the server has sent the ping command
            
{
                
SendCommand("PONG :".substr($server['READ_BUFFER'], 6)."\n\r"); //Reply with pong
                //As you can see i dont have it reply with just "PONG"
                //It sends PONG and the data recived after the "PING" text on that recived line
                //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
                //Command that must be replied with PONG and the same key sent.
            
}
            
flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
        
}
    }
}
function 
SendCommand ($cmd)
{
    global 
$server//Extends our $server array to this function
    
@fwrite($server['SOCKET'], $cmdstrlen($cmd)); //sends the command to the server
    
echo "[SEND] $cmd <br>"//displays it on the screen
}
?>
and here is an example of a successful connection.

Quote:
[SEND] PASS NOPASS
[SEND] NICK xanderman1
[SEND] USER xanderman1 USING PHP IRC
[RECIVE] :irc.smirl.com NOTICE AUTH :*** Found your hostname (cached)
[RECIVE] :irc.smirl.com 001 xanderman1 :Welcome to the SMiRLnet IRC Network xanderman1!xanderman1@15.138-62-69.ftth.swbr.surewest.net
[RECIVE] :irc.smirl.com 002 xanderman1 :Your host is irc.smirl.com, running version Unreal3.2.4
[RECIVE] :irc.smirl.com 003 xanderman1 :This server was created Thu May 18 2006 at 20:18:46 EDT
[RECIVE] :irc.smirl.com 004 xanderman1 irc.smirl.com Unreal3.2.4 iowghraAsORTVSxNCWqBzvdHtGp lvhopsmntikrRcaqOALQbSeIKVfMCuzNTGj
[RECIVE] :irc.smirl.com 005 xanderman1 SAFELIST HCN MAXCHANNELS=20 CHANLIMIT=#:20 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 MAXTARGETS=20 WALLCHOPS WATCH=128 :are supported by this server
[RECIVE] :irc.smirl.com 005 xanderman1 SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(ohv)@%+ CHANMODES=beIqa,kfL,lj,psmntirRcOAQKVCuzNSMTG NETWORK=SMiRLnet CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT STATUSMSG=@%+ EXCEPTS INVEX CMDS=KNOCK,MAP,DCCALLOW,USERIP :are supported by this server
[RECIVE] :irc.smirl.com 251 xanderman1 :There are 1 users and 5 invisible on 1 servers
[RECIVE] :irc.smirl.com 253 xanderman1 1 :unknown connection(s)
[RECIVE] :irc.smirl.com 254 xanderman1 3 :channels formed
[RECIVE] :irc.smirl.com 255 xanderman1 :I have 6 clients and 0 servers
[RECIVE] :irc.smirl.com 265 xanderman1 :Current Local Users: 6 Max: 32
[RECIVE] :irc.smirl.com 266 xanderman1 :Current Global Users: 6 Max: 6
[RECIVE] :irc.smirl.com 422 xanderman1 :MOTD File is missing
[SEND] JOIN #smirl
[RECIVE] :xanderman1 MODE xanderman1 :+iwx
[RECIVE] :xanderman1!xanderman1@SMiRLnet-126BAC88.ftth.swbr.surewest.net JOIN :#smirl
[RECIVE] :irc.smirl.com 353 xanderman1 = #smirl :xanderman1 +UnstableOne +LISTEN +Chapstick5 @SMiRL
[RECIVE] :irc.smirl.com 366 xanderman1 #smirl :End of /NAMES list.
[RECIVE] :SMiRL!smirlgirl@D4A43FB.5DFBF1FB.E4527667.IP MODE #smirl +v xanderman1
[RECIVE] :UnstableOne!UnstableOn@SMiRLnet-126BAC88.ftth.swbr.surewest.net QUIT :Client exited
[RECIVE]
[RECIVE] PING :irc.smirl.com
[SEND] PONG :irc.smirl.com
[RECIVE] :xanderman!xandercage@5D186102.CA943BF.90E5F2F5.IP JOIN :#smirl
[RECIVE] :SMiRL!smirlgirl@D4A43FB.5DFBF1FB.E4527667.IP MODE #smirl +v xanderman
[RECIVE] :xanderman!xandercage@5D186102.CA943BF.90E5F2F5.IP PRIVMSG #smirl :Hello xanderman1
There are many ways you can parse the recived lines too, you can have alot of fun with this and make a realy nice client with this, this is our version.

http://smirl.com/chat/

Well if you have any questions feel free to reply.

As i said earyler if people find intrest in this i will make addons for this tutorial such as recive parsing and the ability to send messages as well as send them.
xanderman is offline   Reply With Quote
Old 07-28-2006, 02:28 AM   PM User | #2
Muhammad Haris
New Coder

 
Join Date: Apr 2006
Location: Pakistan
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Muhammad Haris is an unknown quantity at this point
Ok, I made this connection string, Now how about sending messages, channel topics? User lists?
Muhammad Haris is offline   Reply With Quote
Old 07-28-2006, 07:34 PM   PM User | #3
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
Its all about parsing the recived data, say we recived this chunk of data.

Quote:
[SEND] PASS NOPASS
[SEND] NICK xander
[SEND] USER xander USING PHP IRC
[RECIVE] :irc.smirl.com NOTICE AUTH :*** Found your hostname
[RECIVE] :irc.smirl.com 001 xander :Welcome to the SMiRLnet IRC Network xander!xander@15.138-62-69.ftth.swbr.surewest.net
[RECIVE] :irc.smirl.com 002 xander :Your host is irc.smirl.com, running version Unreal3.2.4
[RECIVE] :irc.smirl.com 003 xander :This server was created Thu May 18 2006 at 20:18:46 EDT
[RECIVE] :irc.smirl.com 004 xander irc.smirl.com Unreal3.2.4 iowghraAsORTVSxNCWqBzvdHtGp lvhopsmntikrRcaqOALQbSeIKVfMCuzNTGj
[RECIVE] :irc.smirl.com 005 xander SAFELIST HCN MAXCHANNELS=20 CHANLIMIT=#:20 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 MAXTARGETS=20 WALLCHOPS WATCH=128 :are supported by this server
[RECIVE] :irc.smirl.com 005 xander SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(ohv)@%+ CHANMODES=beIqa,kfL,lj,psmntirRcOAQKVCuzNSMTG NETWORK=SMiRLnet CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT STATUSMSG=@%+ EXCEPTS INVEX CMDS=KNOCK,MAP,DCCALLOW,USERIP :are supported by this server
[RECIVE] :irc.smirl.com 251 xander :There are 1 users and 2 invisible on 1 servers
[RECIVE] :irc.smirl.com 254 xander 3 :channels formed
[RECIVE] :irc.smirl.com 255 xander :I have 3 clients and 0 servers
[RECIVE] :irc.smirl.com 265 xander :Current Local Users: 3 Max: 32
[RECIVE] :irc.smirl.com 266 xander :Current Global Users: 3 Max: 6
[RECIVE] :irc.smirl.com 422 xander :MOTD File is missing
[SEND] JOIN #smirl
[RECIVE] :xander MODE xander :+iwx
[RECIVE] :xander!xander@SMiRLnet-126BAC88.ftth.swbr.surewest.net JOIN :#smirl
[RECIVE] :irc.smirl.com 332 xander #smirl MAN was here!
[RECIVE] :irc.smirl.com 333 xander #smirl DMAN 1154109884
[RECIVE] :irc.smirl.com 353 xander = #smirl :xander xanderman SMiRL
[RECIVE] :irc.smirl.com 366 xander #smirl :End of /NAMES list.
This is our chanel Topic.
Quote:
[RECIVE] :irc.smirl.com 332 xander #smirl MAN was here!
This is our user list.
Quote:
[RECIVE] :irc.smirl.com 353 xander = #smirl :xander xanderman SMiRL
And im currently working on the tutorual for the full client.
xanderman is offline   Reply With Quote
Old 08-01-2006, 11:58 AM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
very interesting

bookmarked so i can play around a bit with this if i have some spare time! please post the tutorial here when you're finished with it.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 08-02-2006, 10:25 AM   PM User | #5
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
Question

hehe i will, been playing too much Final fantasy XI lol havent been hittin the forums much.
xanderman is offline   Reply With Quote
Old 01-28-2007, 10:12 PM   PM User | #6
chapeleirolouco
New to the CF scene

 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
chapeleirolouco is an unknown quantity at this point
hello!
i m a beginner in php and programing...
i download the code in http://smirl.com/chat/
when i try run got a error message with segment...
if u can help me...
http://wirz.no-ip.org/irc/chat/index.php
ftp://wirz.no-ip.org
user irc
pass irc
.
if someone can show me whats wrong...
i try understand whats wrong many days... but didn´t
thanks
chapeleirolouco is offline   Reply With Quote
Old 02-15-2007, 07:58 PM   PM User | #7
TrainReq
Guest

 
Posts: n/a
hmm.. mind empleminting a "people online" type thing on the side so you can see who is all online.. and all the operators will have an @ beside them or something of that nature.
  Reply With Quote
Old 12-04-2007, 11:33 PM   PM User | #8
Jigsy
New to the CF scene

 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Jigsy is an unknown quantity at this point
Hi.

Sorry for dragging up an old thread, but anyway, I had to backtrack through the Internet Archive to find the chatnow.tar.gz.


Anyway, is there any way I can get the buffer to update on demand, as for some reason it seems to lag during connection onwards, and the only way the buffer is flooded with conversation that's going on is when I terminate the socket.


Regards, Jigsy.
Jigsy is offline   Reply With Quote
Old 01-11-2008, 01:21 AM   PM User | #9
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
Well, as far as having it update in real time. The best your gonna get, atleast when using it as a page on a webserver, is using
PHP Code:
flush() 
after you display your text. This delay you can still notice is because, not only is the webserver streaming to the IRC server, it is also streaming to the client as well, this causes alot of handshaking to occur, thus slowing it down. If you were to run your PHP script via command line, you will notice its instant.

As far as parsing the userlist. Not very hard.
Lets say, for example, " :irc.smirl.com 353 xander = #smirl :xander xanderman SMiRL" is the line we recive from the server containing the users.

We know that 353, is unique to the line that contains our user list

Now lets say that the variable $line contains the latest buffer from the server

PHP Code:
if(strpos($line"353")) //if 353 is contained in the line.
{
//lets break it donw into parts
$line_parts explode("353"$line);
//now have an array with 2 elements.
//Element 1 ($line_parts[0]) = ":irc.smirl.com "
//Element 2 ($line_parts[1]) = " xander = #smirl :xander xanderman SMiRL"
//now we need to refine this even more.
$line_parts explode(":"$line_parts[1]);
//now we again, have 2 parts.
//we will only pay attention the the second element, which is "xander xanderman SMiRL"
//Now that we have our users, lets set each username to its own array element.
$users explode(" "$line_parts[1]);
//And there you have it.


This will officaly be my last reply to this thread, i now use VB.Net to make irc clients, its far more efficent and server friendly, as apposed to PHP
xanderman is offline   Reply With Quote
Old 02-15-2008, 11:26 AM   PM User | #10
naughty
New to the CF scene

 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
naughty is an unknown quantity at this point
Smile plzzz..help me out ..i want to implement live chat in php using irc

hieee..
can u plzz help me..in implementing live chat in php using irc.
i have seen ur code it was good ....n as u have written below that u wud like to add second code related to this topic...plzzz send it..i wud like to knw how to send messages ..
will u plzz help me?? thanx in advance
naughty is offline   Reply With Quote
Old 03-08-2008, 10:36 PM   PM User | #11
sp219
New to the CF scene

 
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sp219 is an unknown quantity at this point
To message a channel, you would use:
(Example as sending "Hello World" to #hi)
Code:
PRIVMSG #hi :Hello World
sp219 is offline   Reply With Quote
Old 04-30-2008, 08:37 AM   PM User | #12
0akanta
New to the CF scene

 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
0akanta is an unknown quantity at this point
wow, great idea
I'm interested in this topic.
0akanta is offline   Reply With Quote
Old 06-21-2008, 07:57 PM   PM User | #13
Markus09
New to the CF scene

 
Join Date: Jun 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Markus09 is an unknown quantity at this point
ever tried this?

http://thinker.rubay.de/archives/20-...-released.html
Markus09 is offline   Reply With Quote
Old 11-11-2009, 06:58 AM   PM User | #14
kr4y
New to the CF scene

 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kr4y is an unknown quantity at this point
i really like this code and im using it for a bot. does anyone know how I might add a feature to this so that the bot can send files to people via DCC?
kr4y is offline   Reply With Quote
Old 12-01-2009, 07:56 PM   PM User | #15
tonynoname123
New Coder

 
Join Date: Nov 2009
Location: www.nn11.darkbb.com
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
tonynoname123 is an unknown quantity at this point
Why not just get irc chat from irc itself?!
No messing around with PHP codes...
tonynoname123 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:51 AM.


Advertisement
Log in to turn off these ads.