This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Login using PHP
Started by 1vannn, 15 May 2013 - 09:15 PMPosted 15 May 2013 - 11:15 PM
Alright, my buddies from school and I have created a private ComputerCraft server on his computer and he is hosting a website from it. Instead of having a password server in game, I want to use MySQL. I know this could be very, very dangerous, but we've been able to block any other IP's coming into the web server to ensure the safety of the passwords. I want to be able to make a PHP script that handles the ComputerCraft input, and relays it to the MySQL server, and it get's feedback, and sends the feedback back to the computer ingame. Could someone help? How could this be accomplished?
Edited on 15 May 2013 - 09:16 PM
Posted 15 May 2013 - 11:27 PM
I can help. Fairly easy.
Give me a few more details as to how CC will send / receive the info.
Going to sleep now so will reply later :P/>
Give me a few more details as to how CC will send / receive the info.
Going to sleep now so will reply later :P/>
Posted 16 May 2013 - 12:13 AM
1. Write("Username: ")
2. Send to server
3. CC expects a reply
2. Send to server
3. CC expects a reply
Posted 16 May 2013 - 12:53 AM
Here I have the solution for you: my OS! Viewing the source code of the API Nexos, doLogin() function
Good Luck!
EDIT: If you have any questions do not hesitate
Good Luck!
EDIT: If you have any questions do not hesitate
Posted 16 May 2013 - 09:14 AM
1. Write("Username: ")
2. Send to server
3. CC expects a reply
Just send the username?
What must be returned back?
Here I have the solution for you: my OS! Viewing the source code of the API Nexos, doLogin() function
Good Luck!
EDIT: If you have any questions do not hesitate
He's wanting the php code too.
Posted 16 May 2013 - 12:54 PM
First and foremost, are you proposing using some type of hashing, when sending traffic to and from the server. This would mainly we done with passwords, which I'm not sure (by what you specified) you are going to incorporate. I can write your PHP code, send it to you and fill in the rest of mandatory variables. Just let me know :)/>
Posted 16 May 2013 - 04:20 PM
If the user does not exist in the database, it'll immediately return an error.1. Write("Username: ")
2. Send to server
3. CC expects a reply
Just send the username?
What must be returned back?Here I have the solution for you: my OS! Viewing the source code of the API Nexos, doLogin() function
Good Luck!
EDIT: If you have any questions do not hesitate
He's wanting the php code too.
Posted 16 May 2013 - 04:33 PM
I'm looking for more of a small tutorial, so then I can work on the rest on my own. :)/>First and foremost, are you proposing using some type of hashing, when sending traffic to and from the server. This would mainly we done with passwords, which I'm not sure (by what you specified) you are going to incorporate. I can write your PHP code, send it to you and fill in the rest of mandatory variables. Just let me know :)/>
Posted 16 May 2013 - 05:38 PM
If the user does not exist in the database, it'll immediately return an error.–snip
LUA:
Spoiler
-- Variables
local server, username = "serveraddress.com"
term.clear()
term.setCursorPos(1,1)
write( "Username: ")
username = read()
local response = http.post(server, "user=" .. username)
local content = response.readAll()
response.close()
-- If you're using a free website host, such as 000webhosts, there might be extra text at the end of each file.
-- This can be removed using gsub
print(content)
PHP:
Spoiler
<?php
$sql = array();
$sql['host'] = '';
$sql['username'] = '';
$sql['password'] = '';
$sql['database'] = '';
$sql['table'] = '';
if (isset($_POST['user'])) {
$user = $_POST['user']; // Put the user's variable into a php variable
$con = mysql_connect($sql['host'],$sql['username'],$sql['password']); // Establish the MYSQL connection
mysql_select_db($sql['database'], $con); // Select the database
// Create a query to see if the user is valid
// the * is so that it selects all column variables
$query = "select * from ".$sql['table']." where username='$user'";
$result = mysql_query($query, $con);
if (!$result || mysql_num_rows($result) == 0) {
echo 'Username does not exist.';
return;
}
$row = mysql_fetch_assoc($result);
// $row now has all the stuff from the database row corresponding with the username entered
// like if the database has a column 'age'
echo $row['age']; // echo the age of the user
} else {
// Accessed on a browser
echo '<h3>Invalid Access!</h3>';
}
?>
Untested.. tell me if there are any errors :)/>
Posted 16 May 2013 - 06:48 PM
Yup, the script works.
Howdo I remove the code at the bottom?
Edit: Solved. http://members.000webhost.com/analytics.php
Howdo I remove the code at the bottom?
Edit: Solved. http://members.000webhost.com/analytics.php
Edited on 16 May 2013 - 05:07 PM
Posted 16 May 2013 - 07:12 PM
Can you help with a registration system please? I'm honestly clueless about this.
Posted 17 May 2013 - 01:31 AM
Never knew you could do that on webhost :o/> haha
Registration uses the insert query.
Note: You will need to add some anti-spam method to prevent spam of multiple users being created
LUA:
PHP:
Shout if there are any errors
Registration uses the insert query.
Note: You will need to add some anti-spam method to prevent spam of multiple users being created
LUA:
Spoiler
-- Variables
local server, username, password = "serveraddress.com"
term.clear()
term.setCursorPos(1,1)
write( "Username: ")
username = read()
write( "Password: ")
password = read()
local response = http.post(server, "user=" .. username, "&pass="password)
local content = response.readAll()
response.close()
print(content)
PHP:
Spoiler
<?php
$sql = array();
$sql['host'] = '';
$sql['username'] = '';
$sql['password'] = '';
$sql['database'] = '';
$sql['table'] = '';
if (isset($_POST['user']) & isset($_POST['pass'])) {
$user = $_POST['user']; // Put the user's variable into a php variable
$pass = $_POST['pass'];
$con = mysql_connect($sql['host'],$sql['username'],$sql['password']); // Establish the MYSQL connection
mysql_select_db($sql['database'], $con); // Select the database
// Create a query to see if the user is already in use
// the * is so that it selects all column variables
$query = "select * from ".$sql['table']." where username='$user'";
$result = mysql_query($query, $con);
if (!$result || mysql_num_rows($result) == 0) {
echo 'User already exists.';
return;
}
// User does not exist, lets add them
$query = "insert into ".$sql['table']."(id,username,password) values('','$user','$pass')";
$result = mysql_query($query, $con);
echo !$result ? "An error has occured" : "Registered!"; // Ternary operator :P/>
} else {
// Accessed on a browser or wrong CC information
echo '<h3>Invalid Access!</h3>';
}
?>
Shout if there are any errors
Posted 17 May 2013 - 05:41 PM
It will not work due to the script not sending the PHP variable correctly. Not sure how to fix it, I've been fiddling around for an hour or so.
Posted 17 May 2013 - 06:18 PM
I've tracked it down to your if statement, now, the problem is that your thingy does not like my MySQL. How might I fix this issue?
Posted 18 May 2013 - 09:48 AM
Thingy does not like MySQL? What do you mean? What is it doing?
Posted 18 May 2013 - 10:26 AM
It's skipping everything and just saying the user exists.
Posted 18 May 2013 - 08:22 PM
What? it doesn't re-add the user if the user already exists.
Posted 19 May 2013 - 12:00 AM
I checked the database, there is nothing in the database, and its still saying the user exists.
Posted 19 May 2013 - 12:25 AM
Oh woops, change
if (!$result || mysql_num_rows($result) == 0) {
toif ($result and mysql_num_rows($result) > 0) {
Posted 19 May 2013 - 01:33 AM
It's 1:30AM for me, I'll try it in the morning.
Posted 19 May 2013 - 09:23 PM
"An error has occured".
Posted 20 May 2013 - 02:18 AM
Your database needs to have these columns:
id - auto_increment, primary, integer
username - text
password - text
What headings does it have at the moment?
id - auto_increment, primary, integer
username - text
password - text
What headings does it have at the moment?
Posted 20 May 2013 - 02:39 PM
Oh, that might be a reason why it didn't work. Give me a second to update the database.
Edit: It works! :o/> Thanks so much Remix!
Edit: It works! :o/> Thanks so much Remix!
Posted 23 May 2013 - 03:14 AM
Never knew you could do that on webhost :o/>/> haha
Registration uses the insert query.
Note: You will need to add some anti-spam method to prevent spam of multiple users being created
LUA:Spoiler
-- Variables local server, username, password = "serveraddress.com" term.clear() term.setCursorPos(1,1) write( "Username: ") username = read() write( "Password: ") password = read() local response = http.post(server, "user=" .. username, "&pass="password) local content = response.readAll() response.close() print(content)
PHP:Spoiler
<?php $sql = array(); $sql['host'] = ''; $sql['username'] = ''; $sql['password'] = ''; $sql['database'] = ''; $sql['table'] = ''; if (isset($_POST['user']) & isset($_POST['pass'])) { $user = $_POST['user']; // Put the user's variable into a php variable $pass = $_POST['pass']; $con = mysql_connect($sql['host'],$sql['username'],$sql['password']); // Establish the MYSQL connection mysql_select_db($sql['database'], $con); // Select the database // Create a query to see if the user is already in use // the * is so that it selects all column variables $query = "select * from ".$sql['table']." where username='$user'"; $result = mysql_query($query, $con); if (!$result || mysql_num_rows($result) == 0) { echo 'User already exists.'; return; } // User does not exist, lets add them $query = "insert into ".$sql['table']."(id,username,password) values('','$user','$pass')"; $result = mysql_query($query, $con); echo !$result ? "An error has occured" : "Registered!"; // Ternary operator :P/>/> } else { // Accessed on a browser or wrong CC information echo '<h3>Invalid Access!</h3>'; } ?>
Shout if there are any errors
Mind if I use this with some of my programs?