Jump to content

Alex

Staff Alumni
  • Posts

    2,467
  • Joined

  • Last visited

Everything posted by Alex

  1. That means to add $row to the end of the $data array.
  2. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306682.0
  3. This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=306573.0
  4. This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=306541.0
  5. That's because you're echoing the array.. isn't what you want to do just not echo this?: echo implode(' ',$_SESSION['words']);
  6. Nope, and probably won't ever. I'm not particularly into them.
  7. 1 would probably be closest to PEAR's naming conventions.
  8. I can't think of any links offhand. You can try googling, but I'm guessing you've already done that. The idea behind it really isn't that complicated. I'll give you an example in PHP seeing as this is a PHP forum and you mentioned that you'll either be doing this in Python (a language I have no experience with) or PHP, but I have to warn you.. I've attempted to use PHP for socket servers in the past and I've discovered that it scales absolutely terribly and isn't a good solution for even relatively small games. I'm guessing it's due to the overhead in the socket functions that when used frequently is exposed. (The scaling issues were noticeable with as little as 5-10 clients who weren't sending packets that fast and all packets were of very small size). Anyway.. Here's how I typically setup my protocols. [first 2 bytes] - a short that defines the length of the packet ["family" byte] - This byte sets the "family" of the packet. An example of a family might be "player" ["action" byte] - This byte sets the "action" and might be something like "move" [counter byte] - This is a counter byte that increments after each packet and is reset at a certain point. It's main purpose is to make sure that someone can't just catch the packet and resend it over and over. Since (assuming you're using some kind of encryption) the person couldn't make their own packets. It's also important to note that the client sends this to "check in" and the packets going from server->client won't contain this byte. [rest of the bytes] What I typically do is create two classes for building and reading packets: "PacketBuilder" and "PacketReader". In PHP you can take advantage of the pack and unpack functions for all your binary packing. For example, packing the number 9745 into a 2 byte representation. In other languages that don't have such utilizes, for example, C/C++, you'll have to do it yourself. It's not hard and can be accomplished through some bit-shifting tricks. Anyway, an example of a PacketBuilder class in PHP could look like this: class PacketBuilder { private $_family; private $_action; private $_data; public function __construct($family, $action) { $this->_family = chr($family); $this->_action = chr($action); } public function addChar($char, $signed = true) { $this->_data .= pack($signed ? 'c' : 'C', $char); } public function addShort($short, $signed = true) { $this->_data .= pack($signed ? 's' : 'S', $short); } public function addInt($int, $signde = true) { $this->_data .= pack($signed ? 'i' : 'I', $int); } public function addString($string) { $this->_data .= $string . "\0"; // null terminated C-style strings } public function get() { return str_split( pack( 's', strlen($this->_data) + 2 ) . someEncodingFunction( $this->_family . $this->_action . $this->_data ), 250 ); } } Most of it is pretty self-explanatory because it's mostly just using the pack PHP function, but some of it is not. In the get() function that returns the packet the use of str_split might be confusing. The reason it's necessary to split the packet into multiple parts if it's large is because of the MTU (Maximum Transmission Unit). You can read more about it in the link provided, but it's not that important for our purposes. All you need to know is that if you attempt to send packets of much more than 250 bytes you'll run into problems. For that reason you'll need to split them up and send them separately. Of course, you're client will have to be prepared to receive packets in parts instead of all at once (it will also need to be prepared to receive packets musted together, but that's another issue). And then your PacketReader class might look something like this: class PacketReader { private $_family; private $_action; private $_counter; private $_valid = true; private $_data; public function __construct($data) { $this->_data = SomeDecodingFunction($data); if(strlen($this->_data) < 3) { $this->_valid = false; } else { $this->_family = ord($this->_data[0]); $this->_action = ord($this->_data[1]); $this->_counter = ord($this->_data[2]); $this->erase(3); } } public function getFamily() { return $this->_family; } public function getAction() { return $this->_action; } public function getCounter() { return $this->_counter; } public function isValid() { return $this->_valid; } public function getChar($signed = true) { if(strlen($this->_data) >= 1) { $char = current(unpack($signed ? 'c' : 'C', $this->_data[0]))); $this->erase(1); return $char; } else { return 0; // error } } /* getShort() and getInt() methods.. */ public function getString() { $nullPos = strpos($this->_data, "\0"); $string = substr($this->_data, 0, $nullPos); $this->erase($nullPos + 1); return $string; } private function erase($c) { $this->_data = substr($this->_data, $c); } } You would have some function to take care of processing the packets coming in, making sure that if you received part of a packet it would wait to get the other parts, split apart packets that arrived mushed together, etc.. Then once that's all done assume that the variable $packet contains the packet data without the first 2 bytes (the length of the packet that was stripped off). You could then do something like this: $reader = new PacketReader($packet); switch($reader->getFamily()) { case PACKET_PLAYER: switch($reader->getAction()) { case PACKET_MOVE: /* Assume that the data coming from the client is in the following manner (ignoring the family/action/counter, which we've already delt with): byte 1-4 (int) - player id (now this isn't technically needed because you should be able to get it from the socket, but for this example we'll assume it comes from the client byte 5-6 (short) - new X coordinate byte 7-8 (short) - new Y coordinate */ $playerThatMovedID = $reader->getInt(); $newX = $reader->getShort(); $newY = $reader->getShort(); /* Now we construct the new packet that will get sent to the other players */ $builder = new PacketBuilder(PACKET_PLAYER, PACKET_MOVE); $builder->addInt($playerThatMovedID); $builder->addShort($newX); $builder->addShort($newY); /* Now we can send this packet to all the right players. */ break; default: // Unknown packet action with family of PACKET_PLAYER break; } break; default: // Unknown packet family break; } Networking certainly isn't the easiest topic in programming and this post doesn't, nor was it intended to, fully go over the entire process. It hopefully just sheds some light on some small parts of it. Namely the act of constructing and reading packets using using binary as your transmission type.
  9. If you want to send data in text like that it makes more sense to me that you would use something like XML instead of creating your own format and parsing mechanism. I'm sure you can find XML parsers in whatever language(s) you'll be working with instead of having to create something from scratch on your own. Not only that, but by using a popular format like XML you make it more portable. Personally when I have to send data back and forth like this I usually send raw bytes. Instead of having to specify that x=something and y=something else I have the receiving end expecting certain data in a certain order. For example, say that I'm processing a certain packet that I know is a "move character" packet (this can be determined from previous bytes in the packet). In my protocol I can specify that the X and Y values are both shorts (2 bytes). If as per our protocol we know that the first thing we're reading is the x value, then we can just read 2 bytes and we have our x value. If we also know that the next thing in the packet is the y value then we can just read two more bytes and then we have that. This way we're sending less data back and forth. That's not to say that XML is a bad method though; there are many games that do use XML for transmitting their data back and forth.
  10. It would be a better idea to have the delay in the client-side JavaScript rather than having the server hang on the processing request.
  11. Nothing jumps out at me initially, but why are you using sleep? That's unnecessary. What you should do is visit the page directly to see if it works. If it does then you know the problem is in your JavaScript.
  12. No. You just can't have any whitespace before [m[session_start[/m] outside of the <?php ?> tags. I'm guessing you have a newline or two before you open your PHP tags.
  13. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=306153.0
  14. This is slightly off topic and won't solve your problem, but you should be using mysql_real_escape_string and not mysql_escape_string which is depreciated.
  15. Including a file over HTTP won't get you the source code, only the output (which in your case is nothing). Try this: include("cookie.php");
  16. if(strpos($currpage, $home) !== false){echo 'test works';} strpos - See example #2
  17. mysqli_query is returning false because your query is failing. You need quotes around $user_password. $query = "SELECT * FROM user_info WHERE password = SHA('$user_password')";
  18. http://danielmclaren.net/2008/01/how-to-get-the-current-pages-url-into-flash
  19. You can use a MySQL JOIN query instead of the nested while loops. Something like this would be the equivalent for your example: SELECT table_user.*, table_other.id FROM table_user JOIN ON (table_user.id = table_other.user_id)
  20. No. When including the remote file all they will get is the output of the file, not the source code.
  21. That would always be true, so it's pretty pointless. What exactly are you trying to prevent? No one can just include your PHP files and get the code; if that's what you're trying to prevent.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.