PHP login/Register errors
Ok I have been asked by my college lecturer to create a login system using PHP. Now I read up about PHP seeing as Im not familiar to it. I used this code and coded some myself.
<?php
session_start();
$username = $_POST ['username'];
$password = $_POST ['password'];
if ($username&&$password) {
$connect = mysql_connect("localhost","root","") or die("Couldn't Connect");
mysql_select_db("phplogin") or die ("Couldn't find Database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'")'
$numrows = mysql_num_rows ($query);
if ($numrows!=0) {
while ($row = mysql_fetch_assoc()) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ( ($username==$dbusername) && ($password==$dbpassword) ) {
echo "you are in,<a href ='member.php'> Click here</a> to enter member page";
$_SESSION['username']=$dbusername;
} else {
echo "Incorrect password";
} else {
die ("That user does not exist");
}
echo $numrows;
} else {
die("Please enter a username and password")
}
?>
This is the Index.php I am using. My login page is simple with 2 fields: Username and Password, with a login and register button. Once I enter details into the fields that I have taken from my database, it displays this at the very top of the page.
Click here to enter member page"; $_SESSION['username']=$dbusername; } else echo "Incorrect password"; } else die ("That user does not exist"); echo $numrows; } else die("Please enter a username and password") ?>
I also have a problem with my register page which has 4 fields that you MUST fill in: Username, Email, Password and Confirm Password, along with a Register button to "Register" as a user.
<?php //Register process $submit = $_POST['submit'];
//form data $name = strip_tags ($_POST['name']); $username = strip_tags ($_POST['username']); $password = md5 (strip_tags ($_POST['password'])); $confirmpassword = strip_tags ($_POST['confirmpassword']); $date = date("Ymd");
if ($submit) {
if ($username&&$password&&$confirmpassword) { //encrypt password $password = md5 ($password); $confirmpassword = md5 ($confirmpassword); if ($password==$confirmpassword) { //check character length if (strlen ($username) >25 || strlen ($name) >25) { echo "length of Username or Name is too long"; } else { //checks password length if (strlen ($password) >20||strlen($password)<5) { echo "password must be between 5 and 20 characters"; } else { //registers user } } } else echo "Your passwords to not match!";
} else echo "Please fill in <b>all</b> feidls!"; } ?>
Your full Name: Choose a Username: Choose a Password: Confirm your Password:
HOWEVER, even before entereing detail to hte fields, this large bulk of code appaears at the top of the page.
25 || strlen ($name) >25) { echo "length of Username or Name is too long"; } else { //checks password length if (strlen ($password) >20||strlen($password)<5) { echo "password must be between 5 and 20 characters"; } else { //registers user } } } else echo "Your passwords to not match!"; } else echo "Please fill in all feidls!"; } ?>
Can someone please tell me what I have done wrong here? I use Xampp to make my database.
You need to have inline comments and executable code on separate lines.
You have this:
//check character length if (strlen ($username) >25 || strlen ...
It's commenting out that if statement. It needs to be this:
//check character length
if (strlen ($username) >25 || strlen ...
You're doing that a few times: //Register process $submit = $_POST['submit']; - $submit will never get defined because it's in a comment.
Here you go. I'm in a good mood. I even added closing </td> s and a <body> tag since those were missing too:
<?php
//Register process
$submit = $_POST['submit'];
//form data
$name = strip_tags($_POST['name']);
$username = strip_tags($_POST['username']);
$password = md5(strip_tags($_POST['password']));
$confirmpassword = strip_tags($_POST['confirmpassword']);
$date = date("Y-m-d");
if ($submit) {
if ($username && $password && $confirmpassword) { //encrypt password
$password = md5($password);
$confirmpassword = md5($confirmpassword);
if ($password == $confirmpassword) {
//check character length
if (strlen($username) > 25 || strlen($name) > 25) {
echo "length of Username or Name is too long";
} else {
//checks password length
if (strlen($password) > 20 || strlen($password) < 5) {
echo "password must be between 5 and 20 characters";
} else {
//registers user
}
}
} else {
echo "Your passwords to not match!";
}
} else {
echo "Please fill in <b>all</b> feidls!";
}
} // end if submit
?>
<html>
<body>
<form action='register.php' method='POST'>
<table>
<tr>
<td> Your full Name:</td>
<td><input type='text' name='name'></td>
</tr>
<tr>
<td> Choose a Username:</td>
<td><input type='text' name='username'></td>
</tr>
<tr>
<td> Choose a Password:</td>
<td><input type='password' name='password'></td>
</tr>
<tr>
<td> Confirm your Password:</td>
<td><input type='password' name='confirmpassword'></td>
</tr>
</table>
<p><input type="button" onclick="window.location.href='member.php'" value="Register"></p>
</form>
</body>
</html>
Well actually it looks like you are calling for it to redirect to form.html but have already outputted some information to the browser. This will cause an error. You will need to try to redirect before outputting anything. So you really should be probably calling valid before you output.
链接地址: http://www.djcxy.com/p/69230.html上一篇: VARIABLE,但这并不意外
下一篇: PHP登录/注册错误
