SA-MP Forums Archive
PHP to JavaScript covert? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: PHP to JavaScript covert? (/showthread.php?tid=295136)



PHP to JavaScript covert? - sciman001 - 05.11.2011

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.


Re: PHP to JavaScript covert? - Calgon - 05.11.2011

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.


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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...


Re: PHP to JavaScript covert? - Norn - 05.11.2011

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?


Re: PHP to JavaScript covert? - Ronaldo_raul™ - 05.11.2011

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!


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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.


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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


Re: PHP to JavaScript covert? - Pinguinn - 05.11.2011

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


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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.


Re: PHP to JavaScript covert? - Norn - 05.11.2011

Use jquery with php = problem solved.


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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 ******)


Re: PHP to JavaScript covert? - Norn - 05.11.2011

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>



Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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?


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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


Re: PHP to JavaScript covert? - Flyfishes - 05.11.2011

As Norn said, jQuery fixes everything!

http://api.jquery.com/load/


Re: PHP to JavaScript covert? - sciman001 - 05.11.2011

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


Re: PHP to JavaScript covert? - Calgon - 05.11.2011

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.