Jump to content

Check out our Community Blogs


Member Since 13 Nov 2008
Offline Last Active Jun 01 2011 02:14 AM

Topics I've Started

PHP socket server - how to make socket client in browser

18 March 2011 - 09:35 AM

Hi all,

I've been creating a TCP socket server in PHP.
I started by reading this tutorial: Writing Socket Servers in PHP and have edited the final script quite a bit.
It's working really well when telnet'ing from terminal and so on.

But my question is how to make a live client side to work in browser?
I've previously made a http server with node.js where I used socket.io on the frontend (and on the backend with node.js) - but it doesn't seem like it's possible to link socket.io with PHP sockets?
- I think I might have misunderstood something (maybe the http server part) - after all, I'm quite new to socket programming.

I've looked into using a hidden flash file for the socket connection - but I'd really like if there's another (maybe Javascripty) way around this?

Any help is appreciated!

- __ak

brainf*ck interpreter in php

16 March 2011 - 07:10 AM

Been trying to make an brainfuck interpreter in PHP.
I got stuck at making nested loops, so looked through the web and read numerous examples written in C, Perl etc.
A lot of them used a stack to keep track of pointer placement when starting loop, and then pop/shift it off when loop is done. So that's the solution I choose.

... for a while.

It took programs like the classic hello world:
>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++
.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

and two I've made myself:
//Prints 99-00 with leading zero
+++++[>+++++++++++<-]>++ //Counter for first digit
>+++++[>+++++++++++<-]>++ //Counter for second digit
>++++++++++[>++++++++++[<<<<.>>.->>-]<<<<->>++++++++++>-] //Loop and print

//Prints 9-0

But when trying to interpret the 99-bottles-of-beer.net example i get a bunch of rubbish - but some of the lines are correct.
Then at some points i starts to break out of my predefined cells, and i get:
PHP Notice:  Undefined offset: 50001 in /var/www/bfint.php on line 32
PHP Notice:  Undefined offset: 50051 in /var/www/bfint.php on line 32

Here's the source, bfint.php
#!/usr/bin/php -q
$f = fopen($_SERVER['argv'][1], 'r')	;
$p = 0; 
$d = $c = $l = array();

//Reading the file, byte by byte and putting into data array
while(($b = fgets($f,2))!==false)
	 $d[] = $b;

//Predefining cells
	$c[] = 0;

$loops = count($d);

for($i=0; $i<$loops; $i++) {
	switch($d[$i]) {
		case '>':
		case '<':
		case '+':
		case '-':
		case '.':
			fputs(STDOUT, chr($c[$p]));
		case ',':
			$c[$p] = ord(fgets(STDIN, 2));
		case '[':
			array_unshift($l, $i);
		case ']':
			($c[$p]>0) ? $i=$l[0] : array_shift($l) ;
fputs(STDOUT, "\n");

json_encode() returns unicode

16 March 2011 - 05:05 AM

Hi all,

I'm currently doing a project where it's possible to type in data in HTML to be imploded to and JSON string in the PHP backend.
The user have two input fields: "Title" and "value".
But "value" could contain an JSON object itself which you have "Title" as index in the major object.

I do this on backend:
            foreach($_POST['jsonArguments'] as $key=>$arg) {
    		$val = $_POST['jsonArguments'][$key];
                    $json_parse = json_decode($val);
                    if($json_parse) $val = $json_parse;
                    $json[$arg] = $val;
            $parser_setting_new = json_encode($json);

And when you save and refresh the page, the saved JSON settings should be show as you wrote them.
The problem comes when I try to use Scandinavian (Danish actually) characters like; æ ø å.
The word "måge" (seagull) becomes "måu005ege" because "å" is replaced with "u005e".

My question is, how to I handle this correct so that I can see the actual characters in my frontend instead of unicodes?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download