javajenius
New Member
- Messages
- 258
- Reaction score
- 0
- Points
- 0
I am trying to authenticate users with MySQL and php instead of using php and a text file.
The problem im having is that i do not understand how users are supposed to connect to the mysql database.
For example it says
If I am correct, you have to connect to a mysql database using a user account on the computer. So everytime someone logs in from the login form, i have to enter my user and password, then look at the database to authenticate the users? Is this correct?
Here is some basic code that I need to modify:
The problem im having is that i do not understand how users are supposed to connect to the mysql database.
For example it says
Code:
mssql_pconnect("localhost","mssqluser","password")
If I am correct, you have to connect to a mysql database using a user account on the computer. So everytime someone logs in from the login form, i have to enter my user and password, then look at the database to authenticate the users? Is this correct?
Here is some basic code that I need to modify:
PHP:
<?
/* SQL REQUIRED FOR THIS SCRIPT *****
create table users (
id INT NOT NULL,
username VARCHAR(16),
password VARCHAR(8),
primary key(id));
*****/
function connect() {
if(!$db = @mssql_pconnect("localhost","mssqluser","password")){ //here is the problem
print("<h1>Cannot Connect to the DB!</h1>\n");
return 0;
} else {
mssql_select_db("php", $db);
return 1;
}
}
function check_user($user, $password) {
if(connect()) {
$password = substr($password, 0, 8);
$sql = "select * from users where username = '$user' and password = '$password'";
$result = mssql_query($sql);
if (mssql_num_rows($result) == 1) {
setcookie("user",$user);
setcookie("password",$password);
return 1;
} else {
?>
<h3>Sorry, you are not authorized!</h3>
<?
return 0;
}
}
}
/***** MAIN *****/
if(!isset($user) or !check_user($user, $password)) {
?>
<h1>You must log in to view this page</h1>
<form action = "DB_authenticate.php" method="post">
<P>Username: <input type="text" name="user"><br>
Password: <input type="password" name="password" maxlength="8" size="8"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?
} else {
?>
<h1>Authorized!</h1>
<?
}
?>