Forum Moderators: coopster
<?php
$dbh=mysql_connect ("localhost", "example", "example") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("example");
session_start();
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql="select * from Users where username='$username' and password='$password'";
$result=mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)!=1){
$error="Login Failed";
include "login.php";
}else{
$row=mysql_fetch_array($result);
$_SESSION['username']="$username";
$_SESSION['ip']=$_SERVER['REMOTE_ADDR'];
header("Location:http://members.example.com/{$row['USLP']}");
}
?>
Resource id #2
Warning: session_start(): Cannot send session cache limiter - headers already sent on Line 5.
Here is the complete login.php file again.
<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("example");
echo ($dbh);
session_start();
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql="select * from users where username='$username' and password='$password'";
exit ($sql);
$result=mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)!=1){
$error="Not a Valid Username or Password";
echo mysql_num_rows ($result);
}else{
$row=mysql_fetch_array($result);
$_SESSION['username']="$username";
$_SESSION['ip']=$_SERVER['REMOTE_ADDR'];
header("Location:http://members.example.com/{$row['USLP']}");
}
?>
Sorry but your script isn't fine, let's have a look:
<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("example");
echo ($dbh);
session_start(); <--- The problem is here
You are trying to echo something BEFORE you start the session. This is a no no, try it this way:
<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("example");
session_start();
echo ($dbh);
Should sort you out
True, you don't normally want to ever echo out anything before starting a session, but we are merely troubleshooting database connection and query syntax at this point. I usually use exit() as opposed to echo() just because I want to stop at that point and check the syntax of the query statement. Here nor there, let's get back to the real issue...
joe1182, try entering a new password for the user "guest" into your database. Make it very simple for right now so that you can key it again into your form to make sure you are using the same password that you generated. Your query syntax looks fine, you obviously have a connection to your database or you would be getting connection errors, therefore the only thing left now is to determine why your query isn't selecting any records. I am willing to bet it is your password/encryption somehow. If you take the AND clause out of your query for a minute, I'll bet your script will continue as expected...
//$sql="select * from Users where username='$username' and password='$password'";Try that.
$sql="select * from Users where username='$username'";
<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");
session_start();
echo ($dbh);
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql="select * from users where username='$username'";
exit ($sql);
$result=mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)!=1){
$error="Not a Valid Username or Password";
echo mysql_num_rows ($result);
}else{
$row=mysql_fetch_array($result);
$_SESSION['username']="$username";
$_SESSION['ip']=$_SERVER['REMOTE_ADDR'];
header("Location:http://members.example.com/{$row['USLP']}");
}
?>
Resource id #2select * from users where username='guest'0
Please correct me if I am wrong. "Resource id #2" this refers to echo ($dbh). "select * from users where username='guest'" this refers to echo ($sql). Last of all "0" this refers to echo mysql_num_rows ($result). Is this correct? Than the "0" just shows that we aren't receiving any records from the database. Correct?
[Added:]
When you echo troubleshooting stuff, try doing it something like this:
echo "\$variable = $variable<br>\n";
...so you can see which varible you've echoed.
$sql="select * from users where username='$username'";
echo "\$sql = $sql<br>\n";
// Regarding echoing, did you read message 21?
When $sql echos out:
$sql = select * from users where username='guest'
...are you absolutely certain that you have a user named 'guest' in the username column of the database? My guess is, "No."
MySQL Database has 3 fields. "username", "password" & "USLP".
I have 2 records in this database.
Record #1 - guest, password, USLP
Record #2 - jmcneil, password, USLP
Is this any help? Is something wrong with the way my database is setup?
echo "mysql_num_rows(\$result) = ".mysql_num_rows($result)."<br>\n";
Is it possible that there are spaces at either end of the current usernames in the database? That might be the problem. Try this test:
$sql = "select * from users";
$result = mysql_query($sql);
while ($query_data = mysql_fetch_array($result)) {
echo "username = '".$query_data['username']."'<br>\n";
}
...by putting $query_data['username'] in single quotes, you'll be able to see if there are spaces at either end of your current users.
Did you look at the HTML source of the output to see what was there?
How did you get the current records into the table? Did you import them from Excel? Maybe the username fields contain an Excel cell? I don't know, I've never imported directly from Excel.
Also, what column type are you using for username? It should probably be char or varchar.
Also , you could try adding a new record. Try putting this line just before the last block I gave you:
$result = mysql_query('INSERT INTO users (username) VALUES('joe')); ...if the INSERT doesn't work, add error checking.
Parse error: parse error, unexpected T_STRING on Line 12.
<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");
session_start();
echo "\$dbh = $dbh<br>\n";
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql = "select * from users";
$result = mysql_query($sql);
while ($query_data = mysql_fetch_array($result)) {
echo "username = '".$query_data['username']."'<br>\n"; }
$result = mysql_query('INSERT INTO users (username) VALUES('joe'));
$result=mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result)!=1){
$error="Not a Valid Username or Password";
echo "\mysql_num_rows ($result) = mysql_num_rows ($result)<br>\n";
}else{
$row=mysql_fetch_array($result);
$_SESSION['username']="$username";
$_SESSION['ip']=$_SERVER['REMOTE_ADDR'];
header("Location:http://members.example.com/{$row['USLP']}");
}
?>