AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sockets -> PHP script problem (https://forums.alliedmods.net/showthread.php?t=106845)

Exolent[jNr] 10-19-2009 15:43

Sockets -> PHP script problem
 
I have this simple plugin to test how to use POST in sockets.
Code:
#include < amxmodx > #include < amxmisc > #include < sockets > new const HOSTNAME[ ] = "exolent.zinkhosting.net"; new const SCRIPT[ ] = "plugin_upload.php"; const PORT = 80; public plugin_init( ) {     register_plugin( "Socket -> PHP Test", "0.0.1", "Exolent" ); } public client_authorized( client ) {     static szName[ 32 ], szAuthid[ 35 ], szIP[ 32 ];     get_user_name( client, szName, 31 );     get_user_authid( client, szAuthid, 34 );     get_user_ip( client, szIP, 31, 1 );         // 1 char = 3 hex places ( %XX )     // ( ( 32 - 1 ) * 3 ) + 1 = 94     // ( ( 35 - 1 ) * 3 ) + 1 = 103     static szFixedName[ 94 ], szFixedAuthid[ 103 ], szFixedIP[ 94 ];     FormatString( szName, szFixedName, 93 );     FormatString( szAuthid, szFixedAuthid, 102 );     FormatString( szIP, szFixedIP, 93 );         log_amx( "Sending data: ^"%s^" ^"%s^" ^"%s^"", szFixedName, szFixedAuthid, szFixedIP );         static szSocketQuery[ 512 ], iDefaultLen;     if( !iDefaultLen )     {         iDefaultLen = formatex( szSocketQuery, 511, "POST /%s HTTP/1.1^r^nHost: %s^r^n^r^n", SCRIPT, HOSTNAME );     }     formatex( szSocketQuery[ iDefaultLen ], 511 - iDefaultLen,\         "name=%s&steamid=%s&ip=%s",\         szFixedName,\         szFixedAuthid,\         szFixedIP         );         new iError, hSocket = socket_open( HOSTNAME, PORT, SOCKET_TCP, iError );     if( hSocket > 0 && !iError )     {         socket_send( hSocket, szSocketQuery, strlen( szSocketQuery ) );                 socket_close( hSocket );     }     else     {         log_amx( "There was an error when creating the socket to %s:%i", HOSTNAME, PORT );                 switch( iError )         {             case 1:             {                 log_amx( "Error when creating socket." );             }             case 2:             {                 log_amx( "Couldn't resolve the hostname." );             }             case 3:             {                 log_amx( "Couldn't connect to given hostname:port." );             }         }     } } stock FormatString( const szInput[ ], szOutput[ ], iLen ) {     static const HEXCHARS[ 16 ] = {         '0', '1', '2', '3', '4', '5', '6', '7',         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'     };         new iPos, cChar, iFLen;     while( ( cChar = szInput[ iPos ] ) && iFLen < iLen )     {         if( cChar == 0x20 ) // 0x20 = space character         {             szOutput[ iFLen++ ] = '+';         }         else if( !( 'A' <= cChar <= 'Z' )         && !( 'a' <= cChar <= 'z' )         && !( '0' <= cChar <= '9' ) )         {             if( ( iFLen + 3 ) > iLen )             {                 break;             }             else if( cChar > 0xFF )             {                 cChar = '*';             }                         szOutput[ iFLen++ ] = '%';                         if( cChar < 16 )             {                 szOutput[ iFLen++ ] = HEXCHARS[ 0 ];             }                         while( cChar > 0 )             {                 szOutput[ iFLen++ ] = HEXCHARS[ cChar % 16 ];                 cChar /= 16;             }         }         else         {             szOutput[ iFLen++ ] = cChar;         }                 iPos++;     } }

Name: "wtk. Exolent[jNr]"
SteamID: "STEAM_0:1:2058929"
IP: "##.##.##.##" (get away from my IP :crab:)

Log Results:
Code:

L 10/19/2009 - 12:27:44: [upload_test.amxx] Sending data: "wtk%E2+Exolent%B5jNr%D5" "STEAM%F50%A31%A32058929" "##%E2##%E2##%E2##"
Here is the PHP script that handles the information:
PHP Code:

<?php

$name 
trim$_POST"name" ] );
$steamid trim$_POST"steamid" ] );
$ip trim$_POST"ip" ] );

$filename "httptest.txt";

$file_handle fopen$filename"a+" );
fputs$file_handle"\"" $name "\" \"" $steamid "\" \"" $ip "\"\n" );
fclose$file_handle );

?>

The problem is in the results of the file that saves the information that was sent:
Code:

"" "" ""
"" "" ""
"" "" ""
"" "" ""

The log messages from the plugin show that the plugin's part in getting the information and preparing the socket and sending data is correct.
But the problem must be within the data being sent through the socket.

Hawk552 10-19-2009 15:56

Re: Sockets -> PHP script problem
 
I believe that _POST is used when gathering data from a form, whereas you would want _GET, which is instead used to gather data from a URL.

EDIT: You have hosting with styles, eh?

AntiBots 10-19-2009 16:36

Re: Sockets -> PHP script problem
 
Wait Amxx cURL :D

Exolent[jNr] 10-19-2009 17:23

Re: Sockets -> PHP script problem
 
Can you give me an example of what to do?
I've done everything I can think of.

I've used:
  • GET
    • Code:

      GET /%s HTTP/1.1^nHost: %s^n^nname=%s&steamid=%s&ip=%s
      Result: None
    • Code:

      GET /%s?name=%s&steamid=%s&ip=%s HTTP/1.1^nHost: %s^n^n
      Result: 404 Not Found
  • POST
    • Same attempts as GET
  • HEAD
    • Same attempts as GET

VMAN 10-19-2009 18:06

Re: Sockets -> PHP script problem
 
I use sockets to match steamid's with my website.

I use GET like you showed
PHP Code:

GET /%s?name=%s&steamid=%s&ip=%s HTTP/1.1^nHost: %s^n^

And I get no 404

Exolent[jNr] 10-19-2009 19:59

Re: Sockets -> PHP script problem
 
Actually, there was a problem with my socket query.
I believe I was putting the host formatted into the wrong section.
So instead of:
Host: exolent.zinkhosting.net^n^n
It was
Host: IP.GOES.HERE.XD^n^n

Anyway, everything works now.

Hawk552 10-20-2009 16:55

Re: Sockets -> PHP script problem
 
Just wondering: does _POST work for data sent by URL, as you've done? I suspect that, if it does, it would be unreliable elsewhere since a browser would handle all of the post/form data internally.

Exolent[jNr] 10-20-2009 17:52

Re: Sockets -> PHP script problem
 
I don't know. I didn't test after I fixed my method with GET.

joropito 10-20-2009 19:27

Re: Sockets -> PHP script problem
 
Quote:

Originally Posted by Hawk552 (Post 967970)
Just wondering: does _POST work for data sent by URL, as you've done? I suspect that, if it does, it would be unreliable elsewhere since a browser would handle all of the post/form data internally.

This is the correct format for POST

Code:

POST /plugin_upload.php HTTP/1.0\n
User-Agent: HTTPTool/1.0\n
Content-Type: application/x-www-form-urlencoded\n
Content-Length: 512\n
\n
name=SomeName&authid=SomeId&reason=SomeText

You must be sure to put the correct Content-Length to avoid compatbility problems with web servers.

Alka 10-21-2009 13:12

Re: Sockets -> PHP script problem
 
Quote:

Originally Posted by AntiBots (Post 967176)
Wait Amxx cURL :D

Hmm, that is a nice tool for transfering files through HTTP/FTP with url syntaxes. When it will be done?


All times are GMT -4. The time now is 17:40.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.