Raised This Month: $ Target: $400
 0% 

Sockets -> PHP script problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-19-2009 , 15:43   Sockets -> PHP script problem
Reply With Quote #1

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 )

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 10-19-2009 , 15:56   Re: Sockets -> PHP script problem
Reply With Quote #2

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?
__________________

Last edited by Hawk552; 10-19-2009 at 15:58.
Hawk552 is offline
Send a message via AIM to Hawk552
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 10-19-2009 , 16:36   Re: Sockets -> PHP script problem
Reply With Quote #3

Wait Amxx cURL
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-19-2009 , 17:23   Re: Sockets -> PHP script problem
Reply With Quote #4

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
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-21-2009 , 13:12   Re: Sockets -> PHP script problem
Reply With Quote #5

Quote:
Originally Posted by AntiBots View Post
Wait Amxx cURL
Hmm, that is a nice tool for transfering files through HTTP/FTP with url syntaxes. When it will be done?
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
VMAN
Senior Member
Join Date: Oct 2007
Location: California, US
Old 10-19-2009 , 18:06   Re: Sockets -> PHP script problem
Reply With Quote #6

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
__________________
VMAN is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-19-2009 , 19:59   Re: Sockets -> PHP script problem
Reply With Quote #7

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 10-20-2009 , 16:55   Re: Sockets -> PHP script problem
Reply With Quote #8

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.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 10-20-2009 , 17:52   Re: Sockets -> PHP script problem
Reply With Quote #9

I don't know. I didn't test after I fixed my method with GET.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 10-20-2009 , 19:27   Re: Sockets -> PHP script problem
Reply With Quote #10

Quote:
Originally Posted by Hawk552 View Post
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.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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