Hi guys, can someone help me out please; I try to put the login page (login.php), index page and register page all on one page. I wonder if that possible or good idea?
Here are my code:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<!--form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"-->
<form action="login.php" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Log in" />
</form> <p>
<a href="register.php">Register?</a>
</body>
</html>
login.php
<?php
$username = (isset($_POST['username'])) ? $_POST['username'] : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
if($username && $password) {
$connect = mysql_connect("localhost", "root", "") or die ("Couldn't connect!");
mysql_select_db("friendsdb") or die ("Couldn't find the DB");
$query = mysql_query ("SELECT * FROM `user` WHERE username = '$username'");
$numrows = mysql_num_rows($query);
if ($numrows != 0){
while ($row = mysql_fetch_array($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//Check to see if they are match!
if ($username == $dbusername && md5($password) == $dbpassword) {
echo "You are in! <a href = 'member.php'> Click </a> here to enter the member page!";
$_SESSION['username'] = $username;
}
else
echo "Incorrect password!";
//code of login
}else
die("That user does not exist!");
//echo $numrows;
}
else
die ("Please enter a username and password!");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
register.php - but on the register.php I got an error saying "Undefined submitok, undefined fullname, undefined username, undefinded password, underfinded repeatpassword.
I know that I need to put "isset" here and wonder if what I did here is correct: Please help me out. Thanks.
$submitok = (isset($_POST['submitok'])) ? $_POST['submitok'] : '';
$fullname = (isset($_POST['fullname'])) ? $_POST['fullname'] : '';
$username = (isset($_POST['username'])) ? $_POST['username'] : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$repeatpassword = (isset($_POST['repeatpassword'])) ? $_POST['repeatpassword'] : '';
<?php
require 'includes/application_top.php';
$submitok = $_POST['submitok'];
//Form data
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date ("Y-m-d");
if ($submitok) {
//open DB
$connect =mysql_connect ("localhost","root","");
mysql_select_db("friendsdb"); //Select DB
$namecheck = mysql_query ("SELECT username FROM `user` WHERE username = '$username'");
$count = mysql_num_rows($namecheck);
//echo $count;
if ($count != 0) {
die("Username already taken");
}
//check for existance
if ($fullname&&$username&&$password&&$repeatpassword) {
if ($password == $repeatpassword){
//Check char length of usename and fullname
if (strlen($username) > 25 || strlen ($fullname) > 25){
echo "Length of username or fullname too long";
}
else
{
//Check password lenght
if (strlen ($password) > 25 || strlen($password) < 6){
echo "Passsword must be between 6 to 25 chars!";
}
else
{
//register the user!
//Encrypted pasword
$password = md5($password);
$repeatpassword = md5($repeatpassword);
//echo "Sucesss!";
//open DB
//$connect =mysql_connect ("localhost","root","");
//mysql_select_db("friendsdb"); //Select DB
$queryreg=mysql_query ("
INSERT INTO user VALUES ('','$fullname','$username','$password','$date')
");
die ("You have been registered! <a href = 'index.php'>Return to login page</a>");
}
}
}else
echo "Your password do not match!";
}else
echo "Please fill in <b>all</b> fields!";
//echo "$username/$fullname/$password";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<h3>New User Registration Form</h3>
<p><font color="orangered" size="+1"><tt><b>*</b></tt></font>
indicates a required field</p>
<form action="register.php" method="POST">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td align="right">
<p>Your full name</p>
</td>
<td>
<input name="fullname" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<p>Choose a username</p>
</td>
<td>
<input name="username" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<p>Choose a password</p>
</td>
<td>
<input name="password" type="password" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<p>Repeat your password</p>
</td>
<td>
<input name="repeatpassword" type="password" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<hr noshade="noshade" />
<input type="reset" value="Reset Form" />
<input type="submit" name="submitok" value="Register" />
</td>
</tr>
</table>
</form>
</body>
</html>