PHP to JavaScript covert?
#1

Hi guys, I made this little thing that gets some basic server stats. I was just wondering, Is it possible to make this code:
Код:
<html>
<?php

getstats();

function getstats()
{
  $ip = "24.111.46.103";
  $port = "7777";
  $fp = fsockopen('udp://' . $ip, $port, $errno, $errstr,0.0);
  if (!$fp)
  {
     echo "offline";
  }
  else
  {
    $packet = 'SAMP';
    $packet .= chr(strtok($ip, '.'));
    $packet .= chr(strtok('.'));
    $packet .= chr(strtok('.'));
    $packet .= chr(strtok('.'));
    $packet .= chr($port & 0xFF);
    $packet .= chr($port >> 8 & 0xFF);

    fwrite($fp, $packet.'i');
    fread($fp, 11);

    $is_passworded = ord(fread($fp, 1));
    $plr_count     = ord(fread($fp, 2));
    $max_plrs      = ord(fread($fp, 2));
    $strlen        = ord(fread($fp, 4));
    $hostname      = htmlentities(fread($fp, $strlen));
    $strlen        = ord(fread($fp, 4));
    $gamemode      = htmlentities(fread($fp, $strlen));
    $strlen        = ord(fread($fp, 4));
    $mapname       = htmlentities(fread($fp, $strlen));
	
	if($is_passworded == 1)
	{
		$is_passworded = "yes";
	}
	else if($is_passworded == 0)
	{
		$is_passworded = "no";
	}	
	
    echo "Hostname  : $hostname " . "<br>";
    echo "Gamemode : $gamemode ". "<br>";
    echo "Map : $mapname ". "<br>";
    echo "Players: $plr_count/$max_plrs". "<br>";
    echo "Passoword: $is_passworded". "<br>";
	
    fwrite($fp, $packet.'c');
    fread($fp, 11);
    fclose($fp);
 }	
}
?>
</html>
Into a JavaScript version of it? Like.. the same exact function, but coded in JavaScript. If it is possible, can you please tell me how? It would mean a lot. Anyway, thanks! Later.
Reply
#2

Yes, it's very possible. You can use node.js to create functions to open a UDP socket, with the dgram (UDP) functions, then use basic javascript to display the content (document.Write(), etc), once you've loaded all the variables you need through the socket.
Reply
#3

I have looked into that and... I think I need to read up. However, if anyone could give me a small example of recieving one piece of data from the samp server... I would probably be able to build from it...
Reply
#4

Quote:
Originally Posted by sciman001
Посмотреть сообщение
I have looked into that and... I think I need to read up. However, if anyone could give me a small example of recieving one piece of data from the samp server... I would probably be able to build from it...
No, ****** has the answers.

Why do you need it javascript that bad anyway?
Reply
#5

Quote:
Originally Posted by sciman001
Посмотреть сообщение
Hi guys, I made this little thing that gets some basic server stats. I was just wondering, Is it possible to make this code:
Код:
<html>
<?php

getstats();

function getstats()
{
  $ip = "24.111.46.103";
  $port = "7777";
  $fp = fsockopen('udp://' . $ip, $port, $errno, $errstr,0.0);
  if (!$fp)
  {
     echo "offline";
  }
  else
  {
    $packet = 'SAMP';
    $packet .= chr(strtok($ip, '.'));
    $packet .= chr(strtok('.'));
    $packet .= chr(strtok('.'));
    $packet .= chr(strtok('.'));
    $packet .= chr($port & 0xFF);
    $packet .= chr($port >> 8 & 0xFF);

    fwrite($fp, $packet.'i');
    fread($fp, 11);

    $is_passworded = ord(fread($fp, 1));
    $plr_count     = ord(fread($fp, 2));
    $max_plrs      = ord(fread($fp, 2));
    $strlen        = ord(fread($fp, 4));
    $hostname      = htmlentities(fread($fp, $strlen));
    $strlen        = ord(fread($fp, 4));
    $gamemode      = htmlentities(fread($fp, $strlen));
    $strlen        = ord(fread($fp, 4));
    $mapname       = htmlentities(fread($fp, $strlen));
	
	if($is_passworded == 1)
	{
		$is_passworded = "yes";
	}
	else if($is_passworded == 0)
	{
		$is_passworded = "no";
	}	
	
    echo "Hostname  : $hostname " . "<br>";
    echo "Gamemode : $gamemode ". "<br>";
    echo "Map : $mapname ". "<br>";
    echo "Players: $plr_count/$max_plrs". "<br>";
    echo "Passoword: $is_passworded". "<br>";
	
    fwrite($fp, $packet.'c');
    fread($fp, 11);
    fclose($fp);
 }	
}
?>
</html>
Into a JavaScript version of it? Like.. the same exact function, but coded in JavaScript. If it is possible, can you please tell me how? It would mean a lot. Anyway, thanks! Later.
Omg thanks for the code..! +reped!
Reply
#6

because i cant figure out how to make the php one dynamic.. like... not refreshing the page, but like... i want it to be so that the numbers can change without refreshing the page. plus its easier for me to set up the look of the page with javascript.
Reply
#7

Quote:
Originally Posted by Ronaldo_raul™
Посмотреть сообщение
Omg thanks for the code..! +reped!
thanks
Reply
#8

You do know that the source reader can read Javascript (and .js files)?
Reply
#9

I dont even know what source reader is... Also, cant someone just put together a small example? It's not that hard if you know what to do. And dude, I am looking on ****** on my second monitor. Just because I want an example doesnt mean that I havent researched/am researching.
Reply
#10

Use jquery with php = problem solved.
Reply
#11

I would, but I havent a clue how... Thats why I'm asking for an example. I will be looking on ****** until the next reply. (so dont tell me to look on ******)
Reply
#12

If you're using it in an iframe you could just:

PHP код:
<html>
<script type="text/javascript">
setInterval ( "reloadPage()", 5000 );

function reloadPage ( )
{
    window.location.reload();
}
</script>
<?php

getstats
("24.111.46.103""7777");

function 
getstats($ip$port)
{
  
$fp fsockopen('udp://' $ip$port$errno$errstr,0.0);
  if (!
$fp)
  {
     echo 
"offline";
  }
  else
  {
    
$packet 'SAMP';
    
$packet .= chr(strtok($ip'.'));
    
$packet .= chr(strtok('.'));
    
$packet .= chr(strtok('.'));
    
$packet .= chr(strtok('.'));
    
$packet .= chr($port 0xFF);
    
$packet .= chr($port >> 0xFF);

    
fwrite($fp$packet.'i');
    
fread($fp11);

    
$is_passworded ord(fread($fp1));
    
$plr_count     ord(fread($fp2));
    
$max_plrs      ord(fread($fp2));
    
$strlen        ord(fread($fp4));
    
$hostname      htmlentities(fread($fp$strlen));
    
$strlen        ord(fread($fp4));
    
$gamemode      htmlentities(fread($fp$strlen));
    
$strlen        ord(fread($fp4));
    
$mapname       htmlentities(fread($fp$strlen));
    
    if(
$is_passworded == 1)
    {
        
$is_passworded "yes";
    }
    else if(
$is_passworded == 0)
    {
        
$is_passworded "no";
    }    
    
    echo 
"Hostname  : $hostname " "<br>";
    echo 
"Gamemode : $gamemode ""<br>";
    echo 
"Map : $mapname ""<br>";
    echo 
"Players: $plr_count/$max_plrs""<br>";
    echo 
"Passoword: $is_passworded""<br>";
    
    
fwrite($fp$packet.'c');
    
fread($fp11);
    
fclose($fp);
 }    
}
?>
</html>
Reply
#13

using it in an iframe? how do i do that? is that how i make the thing that i have in my siggy? wow i am a noob... sorry for noobness, but... i mean... using it in an iframe?
Reply
#14

using it in an iframe? is that the thing in my siggy?
Reply
#15

As Norn said, jQuery fixes everything!

http://api.jquery.com/load/
Reply
#16

sorry for double post. i clicked and it didnt go so i posted again. then it did go.
Reply
#17

No, it's not, and you can't add iframes in to your signature. You couldn't add PHP or Javascript content in your signature either. Only dynamic images, but you can't force them to refresh.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)