Jump to content

Trying to write to socket


sonic1984

Recommended Posts

Hey everyone,

 

I promised a friend to create a php form page that writes the input to his server. I thought that I might be able to make that without knowledge and just using tutorials. But it's not working! :-) Can anybody tell me whats wrong with the code below. All it needs to do is submit the input from the form to a server ip & port using a socket request. The form does not give an error, but the server does not recieve anything either.

 

Thank you so much in advance!

 

Peter

 

<html>
<head>
<style type="text/css">
<!--
.style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style>
</head>

<body class="style1">

<?
// form not yet submitted
if (!$submit)
{
?>

<form action="<? echo $PHP_SELF; ?>" method="post" class="style1">
  <strong> Contactgegevens:<BR>
  </strong><BR>

Contactpersoon: <input type="Text" name="form1" size="15"><BR>
Telefoon: <input type="Text" name="form2" size="15"><BR>
E-mail: <input type="Text" name="form3" size="15"><BR>
<BR>
<strong>Extra:<BR>
</strong><BR>

Werkt dit?: <input type="Text" name="form4" size="15"><BR>

<input type="submit" name="submit" value="Verstuur aanvraag">
</form>

<?
}
else
{
   // form submitted

   // where is the socket server?
   $host="xx.xxx.xx.xxx";
   $port = 1234;

   // open a client connection
   $fp = fsockopen ($host, $port, $errno, $errstr);

   if (!$fp)
   {
      $result = "Error: could not open socket connection";
   }
   else
   {
  // get the welcome message
     fgets ($fp, 1234);
  // write the user string to the socket
     fputs ($fp, $form1, $form2, $form3, $form4);
  // get the result
     $result .= fgets ($fp, 1234);
     // close the connection
  fputs ($fp, "END");
     fclose ($fp);

  // trim the result and remove the starting ?
  $result = trim($result);
  $result = substr($result, 2);

  // now print it to the browser
   }
?>
Server said: <b><? echo $result; ?></b>
<?
}
?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/152202-trying-to-write-to-socket/
Share on other sites

First, use long <?php tags instead of short <? tags - this helps highlight on here PHP code and compatibility on servers as not all have the short tags enabled.

 

You can check for error messages and their code numbers by using the $errno and $errstr variables you specify - echo them out to see them.

I forgot to mention that some web hosts block traffic with fsockopen().

 

I've had this problem in the past on a script I wrote to scan a BF2 server returning the details of all players logged in. The web host hosting the clan's website wouldn't allow traffic but my web space does so I had to host (and still do) the scripts on my web space.

@Yesideez:

 

Thank you very much for taking the time to reply.

 

The PHP form runs on a private unix webserver, so I assume thats okay. The request goes to a Windows server also private and not part of normal hosting.

So I guess fsockopen is fine?

 

Unfortunately I really have no knowledge whatsoever when it comes to PHP programming. I understand what you mean with long and short php tags, but I don't have a clue what your error message and error codes suggestion means or how to do it.

 

Do you happen to know if there is somewhere an out of the box php page with form and submit using a php request? Because maybe the tutorial I followed is completely wrong?

 

Sorry for being such a noob

Sorry for being such a noob

First - don't apologise - we all start somewhere!

 

Try this code:

   $fp = fsockopen ($host, $port, $errno, $errstr);

   if (!$fp)
   {
      $result = "Error: could not open socket connection (".$errno.": ".$errstr.")";
   }

Sorry for being such a noob

First - don't apologise - we all start somewhere!

 

Try this code:

   $fp = fsockopen ($host, $port, $errno, $errstr);

   if (!$fp)
   {
      $result = "Error: could not open socket connection (".$errno.": ".$errstr.")";
   }

 

Thanks.

 

I added/changed the code to above, but unfortunately nothing happens. The form cleans itself when i click submit, no error message at all and nothing is recieved at the other end... what could cause that?

Try this:

   if (!$fp)
   {
      echo "Error: could not open socket connection (".$errno.": ".$errstr.")";
exit;
   }

 

That way we should see the message immediately and the script will terminate allowing us to see the message.

 

If you don't even get that then the error would be somewhere else. I often echo out different numbers now and then so I can trace a script through its steps...

echo '1';
if ($condition) {
  echo '2';
  //do something
} else {
  echo '3';
  //do something else
}

If I get "12" I know my $condition is evaluating to true otherwise if I get "13" I know it's evaluating to false.

Try this:

   if (!$fp)
   {
      echo "Error: could not open socket connection (".$errno.": ".$errstr.")";
exit;
   }

 

That way we should see the message immediately and the script will terminate allowing us to see the message.

 

If you don't even get that then the error would be somewhere else. I often echo out different numbers now and then so I can trace a script through its steps...

echo '1';
if ($condition) {
  echo '2';
  //do something
} else {
  echo '3';
  //do something else
}

If I get "12" I know my $condition is evaluating to true otherwise if I get "13" I know it's evaluating to false.

 

Should I put the PHP part in a seperate "submit.php" file and change

<form action="<?php echo $PHP_SELF; ?>" method="post" class="style1">

to

<form action="submit.php" method="post" class="style1">

?

 

Because when I open the PHP file in firefox and look at the source it does not even show the PHP part?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.