2
« on: February 19, 2012, 05:52:10 PM »
This is what worked. Of course, there may be a more efficient way of doing this, and may require a session to be implemented, but it's a start.
Registration form (could be in HTML code):
echo '<form action="newstudent_process.php" method="post">';
echo '<p><b>Name: </b><input class="input" name="student_name" type="text" size="30" tabindex="1" maxlength="50"></p>';
echo '<p><b>E-mail (This will be your username): </b><input class="input" name="student_e-mail" type="text" size="40" tabindex="2" maxlength="100"></p>';
echo '<p><b>Password: </b><input class="input" name="student_pwd" type="password" size="12" tabindex="3" maxlength="12"></p>';
echo '<input type="submit" name="submint" value="Register" tabindex="4"></p>';
Process the form and add to the DB:
<?php
// C O L L E C T P O S T E D F O R M V A L U E S
$name=$_POST['student_name'];
$email=$_POST['student_e-mail'];
$pwd=$_POST['student_pwd'];
$salt='$2a$07$gi74FUurEWqlEo42f5FmPe5$'; // $2a$07$ and the final $ are used for blowfish encryption
if (!get_magic_quotes_gpc())
{
$name=addslashes($name);
$email=addslashes($email);
}
$pwdsf=addslashes(crypt($pwd, $salt)); // H A S H P A S S W O R D A N D S A L T - for some reason this fails in the if routine above
echo $pwdsf;
// C O N N E C T T O D B
@ $db = new mysqli('localhost', 'user', 'pass', 'DB');
if (mysqli_connect_errno())
{
echo 'Could not connect to db.';
exit;
}
//A D D V A L U E S T O D B
$query = "insert into students values ('','".$name."','".$email."','".$pwdsf."')";
$result = $db->query($query);
// confirmation message
if ($result)
{
echo $db->affected_rows.' student registered'; // F O R T E S T I N G or A D D A S U C C E S S M E S S A G E
}
else
echo '<p>Could not register you, please <a href="register.php"> go back and try again.</a></p>';
/*Prepare an e-mail confirmation of registration
N E E D S T O B E S A N I T I S E D
*/
$query = "select * from students WHERE student_email='".$email."'";
$result = $db->query($query);
$num_results = $result->num_rows;
for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
$name=stripslashes($row['student_name']);
echo '<br />Name: '.$name;
$mail=stripslashes($row['student_email']);
echo '<br />Your e-mail is: '.$mail;
}
$subj = "E-mail subject";
$mesg = 'Thank you for registering ...';
$from = 'From: name@site.com';
mail($mail, $subj, $mesg, $from);
?>
Login form (could be a HTML form with PHP embedded):
echo '<form action="login_process.php" method="post">';
echo '<p><b>Username (registered e-mail): </b><input class="input" name="uname" type="text" size="40" tabindex="2" maxlength="100">.</p>';
echo '<p><b>Password: </b><input class="input" name="pwd" type="password" size="12" tabindex="3"></p>';
echo '<input type="submit" name="submit" value="Log in" tabindex="4"></p>';
Process the form:
<?php
$user=$_POST['uname'];
$user=stripslashes($user);
$pwd=$_POST['pwd'];
// C H E C K F O R P A S S W O R D E N T E R E D
if (!$pwd)
{
echo 'No password';
exit;
}
else
{
// P R E P A R E P A S W O R D F O R D B C O M P A R I S O N
$salt='$2a$07$gi74FUurEWqlEo42f5FmPe5$'; // S A M E S A L T A S R E G I S T R A T I O N
$pwddump=stripslashes(crypt($pwd, $salt));
@ $db = new mysqli('localhost', 'user', 'pass', 'DB');
if (mysqli_connect_errno())
{
echo 'Could not connect to db.';
exit;
}
echo $pwddump; // S H O W H A S H F O R T E S T I N G P U R P O S E S
// P U L L U S E R N A M E A N D P A S S W O R D F R O M D B T O C O M P A R E W I T H V A R I A B L E S
$query = "select * FROM students WHERE student_email='".$user."' AND student_pwd='".$pwddump."'";
$result= $db->query($query);
$num_results = $result->num_rows;
for ($i=0; $i <$num_results; $i++)
{
// A L L T A B L E R O W S L O A D E D F O R F U R T H E R U S E I N M Y C O D E
$row = $result->fetch_assoc();
$dbid=stripslashes($row['student_ID']);
$dbname=stripslashes($row['student_name']);
$dbemail=stripslashes($row['student_email']);
$dbpwd=stripslashes($row['student_pwd']);
}
if ($dbpwd != $pwddump)
{
echo 'Wrong username or password';
}
else
{
// R E S T O F P A G E C O D E
I hope someone finds this useful.