bambinou1980 Posted September 7, 2015 Share Posted September 7, 2015 Hello, I have found this fantastic code here: http://www.funphp.com/?p=33#comment-62497 But I am wondering if all the functions are still working fine with the latest php version. Also I have 2 questions about this script: 1)I am not understanding the "$response='NEW:1", where does the "NEW:1" comes from, I have never seen that before. 2)If I wanted to change the function: $output = ereg_replace("[ \t\n\r]","",$input)."\0"; to preg_replace Would this works? $output = preg_replace("[ \t\n\r]","",$input)."\0"; Thank you Ben /*********function to check new order******************/ function get_new_order() { $con=mysql_connect(HOST, USERNAME, PASSWORD); mysql_select_db(DATABASE, $con); $sql="select OrderId from customer_order where order_Status='0' "; //0 for new order $query=mysql_query($sql,$con); if(mysql_num_rows( $query)>0) { return true; } else return false; } /*************************************/ /********Socket Server*********************/ set_time_limit (0); // Set the ip and port we will listen on $address = '127.0.0.1'; $port = 6789; // Create a TCP Stream socket $sock = socket_create(AF_INET, SOCK_STREAM, 0); // 0 for SQL_TCP // Bind the socket to an address/port socket_bind($sock, 0, $port) or die('Could not bind to address'); //0 for localhost // Start listening for connections socket_listen($sock); //loop and listen while (true) { /* Accept incoming requests and handle them as child processes */ $client = socket_accept($sock); // Read the input from the client – 1024000 bytes $input = socket_read($client, 1024000); // Strip all white spaces from input $output = ereg_replace("[ \t\n\r]","",$input)."\0"; $message=explode('=',$output); if(count($message)==2) { if(get_new_order()) $response='NEW:1'; else $response='NEW:0'; } else $response='NEW:0'; // Display output back to client socket_write($client, $response); socket_close($client); } // Close the master sockets socket_close($sock); Quote Link to comment https://forums.phpfreaks.com/topic/298089-can-this-code-be-made-more-friendly-according-to-the-latest-php-versions/ Share on other sites More sharing options...
QuickOldCar Posted September 7, 2015 Share Posted September 7, 2015 mysql_* functions are deprecated, use pdo or mysqli The code on that page converted to html entities Go through all the codes and replace any html entities ( &characters; ) to their proper characters http://dev.w3.org/html5/html-author/charref This line: if(mysql_num_rows( $query)>0){ should have been: if(mysql_num_rows($query) > 0){ $response='NEW:1' is what that person is saving the response as. ereg_replace() to preg_replace() conversion is a matter of adding a delimeter and also escape anything required with a backslash \ http://php.net/manual/en/reference.pcre.pattern.posix.php Some alternatives: added i for a pattern modifier for case insensitive because POSIX regex was case-insensitive and a + for multiples $output = preg_replace("/[ \t\n\r]+/i","",$input)."\0"; $output = preg_replace("~[\s\t\n\r]+~ i","",$input)."\0"; $output = preg_replace('/\s+/', '', trim($input))."\0"; $output = str_replace(array("\r\n","\r","\n","\t"," "),"",trim($input))."\0"; Quote Link to comment https://forums.phpfreaks.com/topic/298089-can-this-code-be-made-more-friendly-according-to-the-latest-php-versions/#findComment-1520464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.