This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

Website help-sending variables

Started by Exerro, 18 February 2013 - 01:06 AM
Exerro #1
Posted 18 February 2013 - 02:06 AM
I can't figure out how to send variables to websites through cc. I don't really know how websites work and i don't know much about php but i can learn very quickly.
I know about http.post etc and for some reason that is supposed to send the variables to the website which i understand kind of but i'm not sure what i would have to put in the website after that to receive the variables. Does it do it automatically?

For example would

http.post(mywebsite,"$var=\"hello\"")

be able to be used on the website as var or does it not work like that

Please help :P/>
GravityScore #2
Posted 18 February 2013 - 02:12 AM
Sending things to a PHP script is easy - you just have to follow a simple format.

Example (in Lua using http.post):

-- This is the variable I want to send to the PHP script. Notice the spaces and punctuation. This must be encoded
local var = "hello there good sir!"

-- The var inside the var= bit is the name you access this variable with when in PHP
local response = http.post("http://www.example.com/script.php", "var=" .. textutils.urlEncode(var))
local contentsOfHTML = response.readAll()
response.close()

This will send the variable var to the script script.php. In the PHP script, you can access it like this:

<?php
// myvariable is the name of the variable containing the string
// var is the key of the array that we supplied when using http.post in lua
$myvariable = urldecode($_POST["var"]);
echo $myvariable;
?>

If we want to send multiple variables, change the http.post to this:

response = http/post("http://www.example.com/script.php", "var=" .. textutils.urlEncode(var) .. "&amp;secondvar=" .. textutils.urlEncode(secondLuaVariable))

And in PHP:

$myvariable = urldecode($_POST["var"]);
$mysecondvariable = urldecode($_POST["secondvar"]);
echo $myvariable."\n";
echo $mysecondvariable;
Exerro #3
Posted 18 February 2013 - 02:18 AM
ok thanks…is $_POST an automatic variable for whatever it receives from a post function
remiX #4
Posted 18 February 2013 - 02:18 AM
If you have a PHP script that has text boxes, you can do what I mentioned in Mikk809h's thread:

local name = "Your Name"
local email = "YourEmail@email.com"
local message = "This is my message"
local website = "http://Your_website_script_page.com"

local params = string.format("your_name=%s&amp;your_email=%s&amp;your_message=%s&amp;contact_submitted=send", name, email, message)
-- The text box where you would enter your name is called 'your_name'
-- The text box where you would enter your email is called 'your_email'
-- The text box where you would enter your message is called 'your_message'
-- The 'send' button is called contact_submitted which is needed for a php script with a contact form or something.

local res = http.post(website, params) -- attempts to post it

if res then
    print("Success!")
    local content = res.readAll()
    res.close()
    -- This part you would get the success message using string.find or whatever
else
    print("Failed!")
end

EDIT:
ok thanks…is $_POST an automatic variable for whatever it receives from a post function

$_POST is used to collect variables sent from a form with the method 'post'.
http://www.w3schools.com/php/php_post.asp
theoriginalbit #5
Posted 18 February 2013 - 02:18 AM
ok thanks…is $_POST an automatic variable for whatever it receives from a post function
you have to use $_get if your gunna use http.get
Exerro #6
Posted 18 February 2013 - 02:38 AM
yay i got it to work kinda…
website:
Spoiler

<!DOCTYPE html>
<html>
    <body>
    <?php
	   $request = urldecode($_POST["var"]);
	   if($request == "hello"){
		   echo "var=hi";
	   }
	   else{
	   echo "var=bye";
	   }
    ?>
    </body>
</html>  
computer:
Spoiler

local var = "hellod"
function string.split(str,pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
  if s ~= 1 or cap ~= "" then
table.insert(t,cap)
  end
  last_end = e+1
  s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
  cap = str:sub(last_end)
  table.insert(t, cap)
end
return t
end
local response = http.post("http://www.bluetideprograms.netau.net/default.php", "var=" .. textutils.urlEncode(var))
local contentsOfHTML = response.readAll()
response.close()
if type( contentsOfHTML ) == "string" then
contentsOfHTML = string.split( contentsOfHTML, "\n" )
end
for i = 1,#contentsOfHTML do
if string.sub( contentsOfHTML[i], 1, 7 ) == "    var" then
  print( string.sub( contentsOfHTML[i], 9, string.len( contentsOfHTML[i] ) ) )
end
end
Exerro #7
Posted 18 February 2013 - 02:50 AM
now i'm a bit stuck on arrays…how do you add to them?
is there a table.insert() equivalent
remiX #8
Posted 18 February 2013 - 02:54 AM

-- Define an array as '$myArray'
$myArray = array('First index', 'Second', 'Third');

-- Loop through all the things in the array
foreach($myArray as $val) {
	echo $val;
}
Exerro #9
Posted 18 February 2013 - 02:57 AM
i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them
Orwell #10
Posted 18 February 2013 - 02:59 AM
i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them
Look into MySQL. It's rather easy and a very good solution. It's easy to use from PHP.
Exerro #11
Posted 18 February 2013 - 03:04 AM
congrats on the 1000 < posts and whats MySQL? another programming language?
remiX #12
Posted 18 February 2013 - 03:10 AM
i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them

Oh …

i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them
Look into MySQL. It's rather easy and a very good solution. It's easy to use from PHP.

Yes, please don't use files X_X

You're using 000webhost right? They do support PHP with free accounts so go to phpMyAdmin. But first you need to create a PHP database with a username and a password.

In the members area, click on MySQL under 'Software / Services' with the desired database name, username and password. Now go into phpMyAdmin to add tables with different fields etc.

To connect to a MySQL DB:
$con = mysql_connect("webhost", "username", "password") or die('This error message will appear if it failed to connect the the DB'); 
Now that will just initialize the connection, to select a database:
mysql_select_db("database_name", $con) or die(mysql_error());
where database_name is the name of the database you want to send/retrieve information from/to.

From then on, just google the different queries you can do when dealing with databases.
Exerro #13
Posted 18 February 2013 - 03:13 AM
erm….sound confusing….wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l
GravityScore #14
Posted 18 February 2013 - 03:49 AM
erm….sound confusing….wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l

Pleassseeeeee don't go with a plain file solution. It's actually far harder to protect/use, even though it may seem simpler.

@RemiX, you know mysql is depreciated? You're ment to use mysqli.
remiX #15
Posted 18 February 2013 - 04:15 AM
erm….sound confusing….wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l

Pleassseeeeee don't go with a plain file solution. It's actually far harder to protect/use, even though it may seem simpler.

@RemiX, you know mysql is depreciated? You're ment to use mysqli.

Never heard of MySQLI before o_O Well the free website hosting I use doesn't look like they support/offer it.