View Full Version : php login/registration
keyboard
06-16-2011, 10:09 AM
I found a registration and login code for php and sql on the internet however when I tried to use it , it wouldn't work. Should I just post the entire scipts here?
thanks for any help.
jscheuer1
06-16-2011, 11:22 AM
Post a link to the code that you found. And tell us what errors you're getting.
keyboard
06-17-2011, 08:01 AM
http://www.astahost.com/info.php/php-writing-generic-login-register-script_t2659.html
This is where I found the code. I'm not even sure if I configered it right so here's the code I copied
This is the registerUser.php file
<?php
if ($_GET['register'] == 'true') {
registerUser();
}
function registerUser() {
mysql_connect('server', 'username', 'password', 'database');
$username = $_POST['username'];
$password = md5($_POST['password']);
$sql = "INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);";
mysql_query($sql);
}
?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']."?register=true" ?>" method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Register">
</form>
</body>
</html>
and this is the page login.php
<?php
if ($_GET['login'] = true) {
loginUser();
} else {
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF']."?login=true" ?>" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
?>
}
<?php
function loginUser() {
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT fldId, fldPassword FROM tblUsers WHERE fldUsername = '$username';";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if (md5($password) = $row['fldPassword']) {
$_SESSION['loggedin'] = true;
echo "Logged In";
}
}
?>
When I try to test making an account I click the submit button but nothing happens. Note- I did make the sql database.
Thanks for any help
fastsol1
06-17-2011, 11:54 AM
You're not actually connecting the the database
mysql_connect('server', 'username', 'password', 'database');
The info for the connect needs to be changed to the login credentials for your database. The script simply provides your what needs to go in the connect but you need to change it to your needs.
keyboard
06-18-2011, 06:20 AM
This is the code used to make the sql database
CREATE TABLE tblUsers (
fldId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
fldUsername VARCHAR(40) NOT NULL,
fldPassword VARCHAR(40) NOT NULL
);
Is that what you're saying? I'm still really confused. Thanks for any help.
fastsol1
06-18-2011, 01:12 PM
No, you need to change the server, username, password, database to what your login credentials are for your database.
keyboard
06-19-2011, 07:53 AM
How do I do that?
fastsol1
06-19-2011, 12:53 PM
How do you log in to your database now? Are you on localhost or paid hosting? Either way the username, password and such is the info you need to replace what I told you. If you don't have a database setup yet then you have a lot more work to do than what I told you.
keyboard
06-20-2011, 12:53 AM
I am using helio host which has php enabled and sql. I just followed the instructions on the page and used that code up the top of this thred in phpmyadmin to make a database. Is this what you're saying. Also, I don't know what I have to change in the code that you pointed out. I'm very new to this so can you please try to explain it to me simply. Thanks for any help.
fastsol1
06-20-2011, 01:30 AM
I am not sure how many different ways you want me to tell you the same thing. I have showed you and told you which items to change 3 times now.
When you setup your database you would have had to create a username and password for the database, use that info in place of the word username and password in the connect line I showed you.
Maybe your username is transformers and the password is optimusprime, I don't know, only you do. Then change the word database in the connect line to what ever you named the database. As for the word server, that is slightly trickier and you may want to contact you host to get that info since you are having such a hard time figuring this out.
I'd definitely say to contact you host for this info. For example, if phpmyadmin was already set up when you opened your hosting account, you might have never known these details.
Your username and password are probably (but maybe not) the same as those you use to log in to phpmyadmin. The database name should be shown in your phpmyadmin panel. As for "server," different hosts have different ways of configuring them and different ways of allowing access from your sites.
Once you have determined your database credentials, use them in place of the "server, username, password, database" in your code. However, for security reasons, always ********* them when you post your code here (or anywhere online).
On a related subject, mysql_connect() does not accept the database and/or table name as an argument:
// the fourth argument is (boolean)new_link, not the database name.
// if you put anything there (other than FALSE), it will simply be interpreted as "true"
// it won't throw an error, but it won't do what you expect, either.
mysql_connect('servername','username','password','databasename');
// this will have no result
$r = mysql_query("select * from table");
// and this will return:
// " No database selected. "
print mysql_error();
// leave 'databasename' _out_ of mysql_connect()
// you need to use this after you connect:
mysql_select_db('databasename');
//and then you can continue with your queries
keyboard
06-20-2011, 06:02 AM
Thanks for all your help everybody. I think I finally got the registration bit working. However there is another problem. :( For the page Thats meant to log them in I just get this.
Fatal error: Can't use function return value in write context in /home1/keyboard/public_html/login.php on line 37
The code is
<?php
if ($_GET['login'] = true) {
loginUser();
} else {
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF']."?login=true" ?>" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
?>
}
<?php
function loginUser() {
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT fldId, fldPassword FROM tblUsers WHERE fldUsername = '$username';";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if (md5($password) = $row['fldPassword']) {
$_SESSION['loggedin'] = true;
echo "Logged In";
}
}
?>
.
I know I'm not very good at this so thanks for your patience.
is this:
<form action="<? $_SERVER['PHP_SELF']."?login=true" ?>" method="POST"> line 37?
it should be written:
<form action="<?php print $_SERVER['PHP_SELF']."?login=true"; ?>" method="POST">
keyboard
06-20-2011, 10:32 PM
I'm still getting this error
Fatal error: Can't use function return value in write context in /home1/keyboard/public_html/login.php on line 37
I changed what you said. Any help is appreciated
fastsol1
06-20-2011, 11:22 PM
Try this, whoever wrote this didn't reopen the php tag correct after the html form.
<?php
if ($_GET['login'] = true) {
loginUser();
} else {
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF']."?login=true" ?>" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
}
function loginUser() {
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT fldId, fldPassword FROM tblUsers WHERE fldUsername = '$username';";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if (md5($password) = $row['fldPassword']) {
$_SESSION['loggedin'] = true;
echo "Logged In";
}
}
?>
keyboard
06-21-2011, 03:51 AM
Still coming up with the same error. I think I will find a new project to work on.
this does seem to be written a little sloppily. before giving up, however, could you let us know which line is line 37? That would certainly help with troubleshooting.
fastsol1
06-21-2011, 11:58 AM
Or how about the fact that they forgot the semicolon on this line and the echo
<form action="<? $_SERVER['PHP_SELF']."?login=true" ?>" method="POST">
Should be this
<form action="<? echo $_SERVER['PHP_SELF']."?login=true"; ?>" method="POST">
I agree that this is very poorly written. Not even sure how the person got this to work in the first place let alone post it somewhere for others to use. If you want a really good tutorial that uses proper coding practices and security features, check this youtube channel out and look for his login/registration videos - there are a few of them - http://youtube.com/betterphp
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.