Jump to content




Need help : HTTP


  • You cannot reply to this topic
7 replies to this topic

#1 Thib0704

  • Members
  • 93 posts
  • LocationgetServer().getPlayer("Thib0704").getLocation();

Posted 08 July 2013 - 03:41 PM

Hi,
I am trying to make a function that sends a script to my database.
The problem is When I am trying to send a file the PHP scripts dosn't
seem to receive the file But when I am sending the file from a web browser No Problems,
PHP inserts them in the database it works like a charm .
when I print the encoded script on the screen No problems it shows it perfectly

I would like to see if you ( forum pro's )
could fix the problem ?
here is the code :

    function upload(username ,password ,fileName ,filePath)
        if not username then
            return "Username missing"
        end
        if not password then
            return "Password missing"   
        end
        if not fileName then
            return "Please define a Filename"
        end
        if not filePath then
            return "Please define a Filepath"
        end
        if fs.exists(filePath) ~= true then
            return "file not found"
        end
            local file = fs.open(filePath,"r")
            content = file.readAll()
                file.close()
            local password = sha256(password)
            local sFile = textutils.urlEncode(fileName)
local response = http.post(CloudServer.."upload.php?username="..username.."&password="..password.."&filename="..sFile.."&filecode="..textutils.urlEncode(content))
                if response then
                    sResponse = response.readAll()
                    response.close()
                        return sResponse
                else
                    return "Cannot connect to host"
                end   
        end


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 July 2013 - 04:16 PM

If you're using $_POST in your PHP it should be
local response = http.post(CloudServer.."upload.php", "username="..username.."&password="..password.."&filename="..sFile.."&filecode="..textutils.urlEncode(content))
Note it is http.post( url, post_data )

If you're using $_GET in your PHP it should be
local response = http.get(CloudServer.."upload.php?username="..username.."&password="..password.."&filename="..sFile.."&filecode="..textutils.urlEncode(content))
Note it is http.get( url )

#3 Thib0704

  • Members
  • 93 posts
  • LocationgetServer().getPlayer("Thib0704").getLocation();

Posted 08 July 2013 - 05:21 PM

Hi,
Thank's for your response
i'm using urldecode($_GET["filecode"'])
now as I said in my previous post
I can receive the data comming from a webbrowser but not from computercraft
what I mean by that ( sorry English isn't my primary language and i'm only 13 ) :
computercraft HTTP API -> Webserver : dosnt receive the FileCode.
Web Browser -> Webserver : no problem.

here is my php script :
<?php
$username = urldecode($_GET["username"]);
$password = urldecode($_GET["password"]);
$name = urldecode($_GET["filename"]);
$file = urldecode($_GET["filecode"]);
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("SecretUpComingProject :P/>") or die(mysql_error());

// $password = hash("sha256",$password);
$hashedpass = hash("sha512",$password);


$query = mysql_query("SELECT * FROM account WHERE Username = '".$username."'") or die(mysql_error());

if (mysql_num_rows($query) < 1 )
{
    die("Invalid Account");
}

while($row = mysql_fetch_array($query)) {

      if( $row['Password'] == $hashedpass ){

        $files = mysql_query("SELECT Name FROM files WHERE Username = '".$username."'") or die(mysql_error());
           
            if ( mysql_num_rows($files) == 15 ) {
                die("Exeeded File limit");
            }
                   
        $names = mysql_query("SELECT * FROM files WHERE Name = '".$name."'") or die(mysql_error());
           

        while ($sName = mysql_fetch_array($names)){
           
        if( $sName["Username"] == $username ){
       
            if ( mysql_num_rows($names) > 0 ){
                die("Name Already Exists");
            }
        }
        }   

        $response = mysql_query("INSERT INTO `files`(`Name`, `Username`, `Code`) VALUES ('$name', '$username', '$file')") or die(mysql_error());
        echo "Successfuly  uploaded";
        echo $file;

    }
    else {   
    echo "NotLogged";
}

}

?>


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 July 2013 - 05:36 PM

I stand by what I've posted. Either change http.post to http.get, and then on your webpage form use <form action="get" ... > instead of the post in the action... or use post in your webpage and CC and use $_GET instead of $_POST in your PHP. You cannot swap between them at will, they both have different processes of passing the data to your php script.

Side note: please look into using prepared statements for your SQL queries so that we do not have another NDFJay incident. At the current state your code is VERY susceptible to SQL injection...

#5 Thib0704

  • Members
  • 93 posts
  • LocationgetServer().getPlayer("Thib0704").getLocation();

Posted 08 July 2013 - 05:46 PM

By prepared statements you mean Having people injecting PHP code into my database
If yes i'm trying to learn how to make it so this won't happen. If you have any tips
please consider giving me some.
Thank's

and I think i will go with $_POST as I heard they where more secure then $_GET

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 July 2013 - 12:34 AM

Ok firstly, it really is up to you which to go with, because the difference between post and get is well, nothing really more "secure".
The way it works is with get, the data being sent to the website exists in the url, as plaintext for the user to see (and where appropriate characters are encoded to be http safe). On the PHP side all the values within the $_GET variable are already decoded for you from the http safe format.
The difference with post is that the data is not in the url, it is actually in the http header which is not visible to the average user, but more advanced users can still capture this http packet and read its header if they really wanted to. Now on the PHP side the values within the $_POST variable are not decoded, they exist in the raw format they came in, which means you have to decode them yourself (like you're doing).
Now the advantage of the data not appearing in the url is a redundant point when it comes to ComputerCraft because, well, there is no browser or url bar for the user to see, and if you chose to print that data is being sent, then you can just not show them the data with get requests anyway. So in terms of CC, go either one, in terms of web applications, you can still go either one, just depends if you wish to show the get query to the user or not... for example Google uses get, you can see all the url data when you do searches...
Here you go, here is a nice little StackOverflow question on the matter


By prepared statement I mean stopping people from being able to do SQL injection. Since I have recently done this for a boring project at uni, I actually have an example on my computer I can just show you... this is my account registration script ( I've heavily commented it for you :) )

Spoiler

Edited by theoriginalbit, 09 July 2013 - 12:38 AM.


#7 Thib0704

  • Members
  • 93 posts
  • LocationgetServer().getPlayer("Thib0704").getLocation();

Posted 09 July 2013 - 03:16 AM

Wow Thank's for this AMAZING script !
I think i will learn a lot with :)

Will it change something if I change the password encryption to sha512 ? code
I think it would be easier if we PM ourself so I could Post you the code without everyone seeing the :P.

Thank's

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 July 2013 - 03:38 AM

Thanks :)

Changing the hashing algorithm from sha256 to sha512 would not change much, it is a little better, but if you want the best one you use m_crypt or something like that, it takes quite some time to create the hash, but that is the point. Also be aware that a hashing algorithm is not a password encryption. Encryption is very different to hashing.

You may PM if you wish, and it your request I can send you my files for that website the php was from. It is no where near usefully as commented as the above code I gave you, but it is commented.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users