Hello!
So I’m familiar with HTML but I’ve never really messed with PHP. Basically, I’m hosting a game server, and right now I have to manually add everyone’s accounts. Basically I’m trying to set up a registration page so that people can sign up automatically. I have no idea where to even start. Could someone point me in the right direction? Thanks!
In its simplest form
Create a new file register.php and add a normal html form inside. The form should have the fields you need, method post and no action (submits to itself)
add some php before the form. check if you’ve received the required fields and if so hash the password using a secure hash routine and insert the data into your database.
Here is a tut for ya to play with (this is untested but should work)
the database
CREATE TABLE IF NOT EXISTS `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(100) NOT NULL,
`lname` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
index.php
[php]<?php
session_start();
//display errors if form was submitted
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul style="padding:0; color:red;">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form action="reg.php" method="POST">
First Name<br>
<input type="text" name="fname" /><br>
Last Name<br>
<input type="text" name="lname" /><br>
Password<br>
<input type="password" name="password" /><br>
Password Again:<br>
<input type="password" name="password2" /><br>
<input type="submit" value="Register" />
</form>[/php]
reg.php
[php]<?php
//Any time your dealing with registration, login, logout always start a session
session_start();
//setup error reporting
$errmsg_arr = array();
$errflag = false;
// DB configuration
$dbhost = "Database Host";
$dbname = "Database Name";
$dbuser = "Database Username";
$dbpass = "Database Password";
// DB Connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// gather the data
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$pass = $_POST['password'];
$pass2 = $_POST['password2'];
//Validate that the passwords match
if(!$pass == $pass2) {
$errmsg_arr[] = "Your passwords don't match";
$errflag = true;
}else{
// Encrypt Password
$hash = sha1(md5($pass));
}
//validate the input and create an error message
if($fname == '') {
$errmsg_arr[] = 'You must enter your First Name';
$errflag = true;
}
if($lname == '') {
$errmsg_arr[] = 'You must enter your Last Name';
$errflag = true;
}
if($pass == '') {
$errmsg_arr[] = 'You must enter your Password';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
}
// query
$sql = "INSERT INTO members (fname,lname,password) VALUES (:1,:2,:3)";
$q = $conn->prepare($sql);
$q->execute(array(':1'=>$fname,':2'=>$lname,':3'=>$hash));
header("location: index.php");
?>[/php]