I have the following code:
$check1 = fsockopen("111.222.333.444","8080");
if ($check1) {
echo ("true");
$close = fclose($check1);
}
else
echo ("false");
How do I get it back to me this value true and false in a string?
Example:
if($check1 == "false") {
// Conecct SSH
if($check1 == "true") {
// Not connect SSH
You need more parameters:
$check1 = fsockopen($host, $port, $errno, $errstr);
if (!$check1) { // $check1==false
echo $errno . ' ' . $errstr
} else { // $check1==true
//Your code
}
Related
I'm creating a method to validate if an email exists.
I need to do this:
Insert several emails in a spreadsheet, save and in the html page, through a function 'file.onchange = function', select this file to validate through this check:
<?php
define ('DEBUG_OK', false);
class CCheckMail
{
var $timeout = 10;
var $domain_rules = array ("aol.com", "bigfoot.com", "brain.net.pk", "breathemail.net",
"compuserve.com", "dialnet.co.uk", "glocksoft.com", "home.com",
"msn.com", "rocketmail.com", "uu.net", "yahoo.com", "yahoo.de");
function _is_valid_email ($email = "")
{ return preg_match('/^[.\w-]+#([\w-]+\.)+[a-zA-Z]{2,6}$/', $email); }
function _check_domain_rules ($domain = "")
{ return in_array (strtolower ($domain), $this->domain_rules); }
function execute ($email = "")
{
if (!$this->_is_valid_email ($email))
{ return false; }
$host = substr (strstr ($email, '#'), 1);
if ($this->_check_domain_rules ($host))
{ return false; }
$host .= ".";
if (getmxrr ($host, $mxhosts[0], $mxhosts[1]) == true)
{ array_multisort ($mxhosts[1], $mxhosts[0]); }
else
{
$mxhosts[0] = $host;
$mxhosts[1] = 10;
}
if (DEBUG_OK) { print_r ($mxhosts); }
$port = 25;
$localhost = $_SERVER['HTTP_HOST'];
$sender = 'info#' . $localhost;
$result = false;
$id = 0;
while (!$result && $id < count ($mxhosts[0]))
{
if (function_exists ("fsockopen"))
{
if (DEBUG_OK) { print_r ($id . " " . $mxhosts[0][$id]); }
if ($connection = fsockopen ($mxhosts[0][$id], $port, $errno, $error, $this->timeout))
{
fputs ($connection,"HELO $localhost\r\n"); // 250
$data = fgets ($connection,1024);
$response = substr ($data,0,1);
if (DEBUG_OK) { print_r ($data); }
if ($response == '2') // 200, 250 etc.
{
fputs ($connection,"MAIL FROM:<$sender>\r\n");
$data = fgets($connection,1024);
$response = substr ($data,0,1);
if (DEBUG_OK) { print_r ($data); }
if ($response == '2') // 200, 250 etc.
{
fputs ($connection,"RCPT TO:<$email>\r\n");
$data = fgets($connection,1024);
$response = substr ($data,0,1);
if (DEBUG_OK) { print_r ($data); }
if ($response == '2') // 200, 250 etc.
{
fputs ($connection,"data\r\n");
$data = fgets($connection,1024);
$response = substr ($data,0,1);
if (DEBUG_OK) { print_r ($data); }
if ($response == '2') // 200, 250 etc.
{ $result = true; }
}
}
}
fputs ($connection,"QUIT\r\n");
fclose ($connection);
if ($result) { return true; }
}
}
else
{ break; }
$id++;
}
return false;
}
}
?>
However, I am not able to create the structure to validate.
I had created an array with some emails inserted in it, however, it only validates when executing the page, if the emails are valid or not, example:
<?php
include ("CCheckMail.php");
$checkmail = new CCheckMail ();
$emails = array ("pedroiagobucci#gmail.com", "invalid#email.com", "setec#freemail.it", "thisdoesentexist#google.com");
foreach ($emails as $email)
{
if ($checkmail->execute ($email))
{ print ("Address $email exists!<br>"); }
else
{ print ("Address $email <strong>not</strong> exists!<br>"); }
}
?>
can you help me?
I am practicing socket programming using stream methods in PHP.
I am using PHP 7.3.11.
I found very unexpected error message which says "undefined index " error where
Here is my server side code
<?php
error_reporting(E_ALL);
/* allow the script to hang around waiting for connection.*/
set_time_limit(0);
$address = '127.0.0.1';
$port = 9016;
$server = stream_socket_server($address .":". $port, $errno, $errorMessage);
$req = array( "R004"=>"R004 exist"
, "R001" => "R001 Ruccess"
, "R002"=>"R002Rcontinue"
, "R003"=>"R003 lRquit"
, "R005"=>"R005 hello");
$res = array("TT","SOO1", "S002", "S003");
if ($server === false)
{
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}
echo json_encode($req);
while($socket = stream_socket_accept($server)){
$rand = rand(1,3);
$peer = stream_socket_get_name($socket, true);
$pkt = stream_socket_recvfrom($socket, 1500, 0,$peer);
if( !empty($pkt)){
$pkt = trim(preg_replace('/\s+/','',$pkt));
echo "pkt[{$pkt}]";
echo "EXITS:".array_key_exists($pkt, $req)."\n";
echo "pkt[{$pkt}]".PHP_EOL;
echo $req[$pkt].PHP_EOL;
stream_socket_sendto($socket, $res[$rand], 0, $peer);
}
fclose($socket);
usleep(1000);
}
stream_socket_shutdown($server, \STREAM_SHUT_RDWR);
and here is my client side code
error_reporting(E_ALL);
set_time_limit(0);
$address = '127.0.0.1';
$port = 9016;
$local = "tcp://{$address}:{$port}";
$socket = stream_socket_client($local, $errno, $errstr, 30);
$c_res = array("S001" => "Success", "S002" => "continue", "S003" => "quit");
$c_req = array("ROO3", "R005", "R002", "R001", "R004");
while ($socket = stream_socket_client($local, $errno, $errstr, 30)) {
$rand = rand(1, 4);
// echo "SEND[$c_req[$rand]]" . PHP_EOL;
$sent = stream_socket_sendto($socket, $c_req[$rand] . PHP_EOL);
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
when I ran this code both server and client ( server runs first and thereafter does client)
I found this kind of error message
PHP Notice: Undefined index: SOO1 in E:\xampp\htdocs\mave_gen\www\adm\client.php on line 29
I do not understand this error message at all because all other references prints out OK except S001.
Obviously, there is S001 is defined in the code.
Do I miss something here ?
Thank you in advance.
The line SOO1 should be S001 in $res = array("TT","SOO1", "S002", "S003"); in the server side code. You have entered letter "O" in SOO1 but it should be "0" (zero) according to the rest of your code.
And in the client side code modify the following line of code
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
to the following line
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
if(!empty($s_res))
{
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
}
After these changes it should work.
I have an error in my code when I compile :
Fatal error: Uncaught ArgumentCountError: Too few arguments to
function getData::QueryWhoisServer(), 0 passed in
C:\xampp\htdocs\testVisitor\index.php on line 19 and exactly 2
expected in C:\xampp\htdocs\testVisitor\Model\getData.php:72 Stack
trace: #0 C:\xampp\htdocs\testVisitor\index.php(19):
getData->QueryWhoisServer() #1 {main} thrown in
C:\xampp\htdocs\testVisitor\Model\getData.php on line 72
I know that since php 7.0 I need to pass argument but the argument are not recognize...
here is my code:
index.php :
require_Once('Model/getData.php');
require_Once('Controller/writeData.php');
$getData = new getData();
$writeData =new writeData();
$getData->get_ip();
$getData->LookupIP($domain);
$getData->ValidateIP($domain);
$getData->QueryWhoisServer();
if($domain && $pageEnCours != preg_match("#localhost/testVisitor/$#",$pageEnCours)) {
$domain = trim($domain);
if($getData->ValidateIP($domain)) {
$result = $getData->LookupIP($domain);
$writeData->write_domain($result);
}
else{
write_error();
};
}
echo $domain;
echo "cc";
and getData.php :
$urlPart1 = $_SERVER['HTTP_HOST'] ;
$urlPart2 = $_SERVER['REQUEST_URI'];
$pageEnCours = $urlPart1 .= $urlPart2;
$domain ='0.0.0.0';
class getData
{
// For the full list of TLDs/Whois servers see http://www.iana.org/domains/root/db/ and http://www.whois365.com/en/listtld/
/**
* Récupérer la véritable adresse IP d'un visiteur
*/
function get_ip() {
// IP si internet partagé
global $domain;
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
return $domain =$_SERVER['HTTP_CLIENT_IP'];
}
// IP derrière un proxy
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $domain=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Sinon : IP normale
else {
return $domain=(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
}
}
function LookupIP($ip) {
$whoisservers = array(
//"whois.afrinic.net", // Africa - returns timeout error :-(
//"whois.lacnic.net", // Latin America and Caribbean - returns data for ALL locations worldwide :-)
//"whois.apnic.net", // Asia/Pacific only
//"whois.arin.net", // North America only
//"whois.ripe.net" // Europe, Middle East and Central Asia only
);
$results = array();
foreach($whoisservers as $whoisserver) {
$result = QueryWhoisServer($whoisserver, $ip);
if ($result && !in_array($result, $results)) {
$results[$whoisserver] = $result;
}
}
$res = "RESULTS FOUND: " . count($results);
foreach($results as $whoisserver=>$result) {
$res .= "\n\n-------------\nLookup results for " . $ip . " from " . $whoisserver . " server:\n\n" . $result;
}
return $res;
}
function ValidateIP($ip) {
$ipnums = explode(".", $ip);
if(count($ipnums) != 4) {
return false;
}
foreach($ipnums as $ipnum) {
if(!is_numeric($ipnum) || ($ipnum > 255)) {
return false;
}
}
return $ip;
}
function QueryWhoisServer($whoisserver , $domain ) {
$port = 43;
$timeout = 10;
$fp = #fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
//if($whoisserver == "whois.verisign-grs.com") $domain = "=".$domain; // whois.verisign-grs.com requires the equals sign ("=") or it returns any result containing the searched string.
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
$out .= fgets($fp);
}
fclose($fp);
$res = "";
if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
$rows = explode("\n", $out);
foreach($rows as $row) {
$row = trim($row);
if(($row != '') && ($row{0} != '#') && ($row{0} != '%') && ($row != preg_match("#^netname|^descr|^country|^person|^address|^phone#",$row ))) {
$res .= $row."\n";
}
}
}
return $res;
}
}
Too few arguments to function getData::QueryWhoisServer(), 0 passed in C:\xampp\htdocs\testVisitor\index.php
$getData->QueryWhoisServer(); is not providing any arguments.
In your function :
function QueryWhoisServer($whoisserver , $domain ) {
$port = 43;
$timeout = 10;
$fp = #fsockopen($whoisserver, $port, $errno, $errstr, $timeout)
fputs($fp, $domain . "\r\n");
$fp need $whoisserver,$portand$timeout
The $port and $timeout are defined in the function
But you need to specify $whoisserver and the $domain (domain is used in fputs) when you call this function,
That will be something like :
$getData->QueryWhoisServer($whoisserver, $domain);
Also in your function
LookupIP($domain);
The result use the fucntion QueryWhoisServer, so try to get the $result
I have a problem with my website (http://www.zurfaria.ga/1/), where I'm trying to check how many players are online. I have downloaded this GitHub repository (https://github.com/mattvh/MCServerStatus) so I can query my server, but I can't seem to echo it in my HTML Here is my HTML and the PHP
<?php
require_once('/onlinecheck/Server.php')
require_once('/onlinecheck/Stats.php')
require_once('/onlinecheck/StatsException.php')
class MCServerStatus {
public $server;
public $online, $motd, $online_players, $max_players;
public $error = "OK";
function __construct($url, $port = '25565') {
$this->server = array(
"zurfaria.fmc.pw" => $url,
"25565" => $port
);
if ( $sock = #stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) ) {
$this->online = true;
fwrite($sock, "\xfe");
$h = fread($sock, 2048);
$h = str_replace("\x00", '', $h);
$h = substr($h, 2);
$data = explode("\xa7", $h);
unset($h);
fclose($sock);
if (sizeof($data) == 3) {
$this->motd = $data[0];
$this->online_players = (int) $data[1];
$this->max_players = (int) $data[2];
}
else {
$this->error = "Cannot retrieve server info.";
}
}
else {
$this->online = false;
$this->error = "Cannot connect to server.";
}
}
}
?>
<h1>Players Online:</h1>
<?php
echo $server->online_players; //Outputs the number of players online
?>
The HTML in between this code is fine, it works. What can I do to fix this? (I've looked up how to use the echo but didn't really understand it...)
I am creating a socket script that will listen to our 3 devices.and the device is setup in one server ip and one port only.
$file = fopen('txt.log','a+');
$server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);
if(!$server) {
echo "$errorMessage ($errno)<br />\n";
}
else{
while($client = #stream_socket_accept($server,$timeout)) {
stream_copy_to_stream($client, $file);
fclose($file);
fclose($client);
}
}
but the problem is that if one device is connected,the two devices cannot connect anymore.I appreciate some one can help me how to get this work.or give me some idea
Thank you in advance.
$file = fopen('txt.log', 'a+');
$server = stream_socket_server("tcp://$ipserver:$port", $errno, $errorMessage);
if (!$server)
echo "$errorMessage ($errno)<br />\n";
else
{
$s = array($server);
$t = $timeout == -1 ? NULL : $timeout;
while ($r = $s and stream_select($r, $n=NULL, $n=NULL, $t))
foreach ($r as $stream)
if ($stream == $server) // new client
$s[] = stream_socket_accept($server, -1);
else
if (!fputs($file, fgets($stream)))
{
fclose($stream);
array_splice($s, array_search($stream, $s), 1);
}
}