SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: php vs. cgi

  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php vs. cgi

    Is it possible to use php to connect several users to each other like in a chat or card game for example. In other way one script should send and receive information to several people and it should run as long as some of them is online. As I understand it is possible with cgi. But can it be done with php?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,018
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    It can be done with any server side programming language that provides some sort of ability to maintain state. ie: Programatically it needs to be able to count the number of player and keep them in memory from page to page or each time a page is refreshed over time.

    You will want to become familiar with Session variables and how they work.

    Because your application may need to refresh itself and count the number or id's or individuals participating you might want to use something like flash as an interface. This will allow the back-end page to be refreshed without any noticable flickering on the front end.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am familiar with sessions. Yes, probably if I use the same session id for all users (say in a chat room) it will be possible for the users to share some information (say messages in a chat room). Is it what you mean? If so, isn't it possible that some conflict will arise if several users have the same user id. For example if several users are writing to the same file there will be nothing good out of it. Are sessions safe in this sense?

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And also, if some user sends a message to the chat room, I want all others to see it immidiately. So I need to send something to other users' browsers from the script and not wait until they refresh the page on their own. Do you know with what functions it can be done. In principle, I can place refresh metatag in .html file, but I think it is not the best solution.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,018
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I built a chat years ago using sessions (unique for each participant) and then used a text file for the transcript of the chat. Each user's input into the discussion was appended to the transcript.

    I don't have the code anymore but that's the gist of it.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  6. #6
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is possible to do it using file system. But it is evidently much more resource-consuming solution than keeping both script and the chat in memory. This is actually what I was intrested in, doing it without having to store anything on the hard disk? It seems to me that it is possible with cgi but not with php. Is it so?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,018
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Nope.

    You're getting hung up on cgi which isn't anything in particular. It's an acronym for Common Gatway Interface and is the method that you would use to execute a PERL scrip on a webserver through an html page.

    CGI is fine if that's all you have but... depending on what you're doing it can be much slower than PHP, ASP, .NET, CFM, JSP, etc....

    Running everything in memory is probably going to put more strain on your resources than using the file system and some lightweight sessions because you will be puttting everything into ram. File System is going to be a lighter weight solution than a database as well.

    If I were going to build this thing, I would build it in PHP with a flash front end using the file system for my transcript (dynamically named with date/time) and use either sessions or cookies to identify my participants.

    I would look HERE

    Or HERE

    For some sripts to work with.

    Experimental
    You do have another option and it goes back to Flash MX. It is entirely possible to build your application in Flash and use actionscript to access XML for your transcript. I guess that you would then be ofsetting the files system work to the flash file that would be running on the client. You would still need to use a session for each participant but that's lightweight.

    Cheers,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  8. #8
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean that using file system will be faster than making everything in memory? It is really surprising. Also if it is about chat room you dont need to keep all the chat in memory. Only the part of it that was not yet sent to all participants, i.e. hardly more than 1kb. So even if you have 100 chats at a time, it will not take more than 100kb in the memory. And I think that any server probably has enough memory for this.

    Also as I remember (though I am not absolutely sure) when I installed php, the installation program offered me to install it as cgi binnary. So if php itself runs as cgi binnary how can it be faster than cgi? (Again I am not sure whether I remember everithing correctly).

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,018
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ilyalyu
    Do you mean that using file system will be faster than making everything in memory? It is really surprising.
    No. Memory is always faster than file system or DB but.... depending on how many visitors you have at the time you could run low on resources if you keep everything in memory.

    Quote Originally Posted by ilyalyu
    Also if it is about chat room you dont need to keep all the chat in memory. Only the part of it that was not yet sent to all participants, i.e. hardly more than 1kb. So even if you have 100 chats at a time, it will not take more than 100kb in the memory. And I think that any server probably has enough memory for this.
    Where are you going to keep the transcript? If you don't keep it in session you'll loose it.

    Quote Originally Posted by ilyalyu
    Also as I remember (though I am not absolutely sure) when I installed php, the installation program offered me to install it as cgi binnary. So if php itself runs as cgi binnary how can it be faster than cgi? (Again I am not sure whether I remember everithing correctly).
    Are you running on a windows server? As I recall the cgi option is only available on windows and it is slower but as I mentioned earlier CGI is the method with which you execute your script and I think you were refering to PERL/CGI in your earlier post. The bottom line is it doesn't matter what server side language you use... You can achieve your goals through PERL, PHP, ASP, .NET, JSP, CFM...
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •