| | |
Login problem, need fresh set of eyes.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
Hey guys, i've got a right headache from this piece of code at the moment, it's a simle login script and it just sin't working, the MySQL query looks correct to me and all the other stuff looks ok, but then again i'm no PHP expert.
Could someone look at it for me please?
Cheers
Could someone look at it for me please?
PHP Syntax (Toggle Plain Text)
<? include ('./includes/header.php'); // Check if the form has been submitted. if (isset($_POST['submitted'])) { require_once('../sqlconnect/connect.php'); $errors = array(); // Initialize error array. // Check for an email address. if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $em = trim($_POST['email']); } // Check for a password. if (empty($_POST['pass'])) { $errors[] = 'You forgot to enter your password.'; } else { $pw = trim($_POST['pass']); } if (empty($errors)) { // If everything's OK. $query = "SELECT * FROM members WHERE email = '$em' AND password = SHA('$pw')"; $result = @mysql_query($query); // Run the query. $row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable. if ($row){ // A record was pulled from the database. //set session session_name('visit'); session_start(); $_SESSION ['id'] = $row[0]; $_SESSION ['name'] = $row[1]; $_SESSION ['email'] = $row[3]; $_SESSION ['agent'] = md5($_SERVER['HTTP_USER_AGENT']); // Redirect the user to the loggedin.php page. // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Add the page. $url .= '/loggedin.php?' . $_SESSION['agent']; header("Location: $url"); exit(); // Quit the script. } else { // No record matched the query. $errors[] = 'The email address and password entered do not match those on file.'; // Public message. $errors[] = mysql_error() . '<br />Query: ' . $query; // Debugging message. } } // End of if (empty($errors)) IF. mysql_close(); // Close the database connection. } else { // Form has not been submitted. $errors = NULL; } // End of the main Submit conditional. //print errors if (!empty($errors)){ echo '<h1 id = mainhead>Error!</h1> <p class = error >Following occured:<br/>'; foreach ($errors as $msg){ echo " - $msg<br/>\n"; } echo '<p>Please try again <a href = login.php>Reset</a>'; } ?>
Cheers
Can you let us know what the problem it is your having??
If my post is useful please add to my reputation.
Thanks.
Ajtrichards Web Solutions | http://www.ajtrichards.co.uk
Retenovate | http://www.retenovate.com
Thanks.
Ajtrichards Web Solutions | http://www.ajtrichards.co.uk
Retenovate | http://www.retenovate.com
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
Oh yea oops, man i feel stupid....
Well, it just doesn't log in, it just comes up with the error "The email address and password entered do not match those on file.". I'm not sure what the real error is though, how do i find out?
This:
Is just coming up with what my query was, which seems to me to be correct. How do you find out the exact error?
Cheers
Well, it just doesn't log in, it just comes up with the error "The email address and password entered do not match those on file.". I'm not sure what the real error is though, how do i find out?
This:
PHP Syntax (Toggle Plain Text)
$errors[] = mysql_error() . '<br />Query: ' . $query; // Debugging
Cheers
•
•
Join Date: Jan 2008
Posts: 136
Reputation:
Solved Threads: 19
Hi,
Whilst developing a new script, I always find it helpful to have PHP error messages turned on. The setting for this can be found in the php.ini file on your server/computer.
Also, it might be worth printing to the screen your SQL query before you execute it and then to die the script.
echo $query;
die;
This will allow you to check the query is correct. And also, you can run it on the MySQL command line or in phpMyAdmin, etc to see if it does actually find a result.
Another obvious one to check, but I made this error before and it took me a while to figure it. Make sure you're hashing the password stored in the database. Once I had a plain text password stored and I was hashing the password for use in the SQL query and I was wondering why it wasn't working - duh!
Best,
R.
Whilst developing a new script, I always find it helpful to have PHP error messages turned on. The setting for this can be found in the php.ini file on your server/computer.
Also, it might be worth printing to the screen your SQL query before you execute it and then to die the script.
echo $query;
die;
This will allow you to check the query is correct. And also, you can run it on the MySQL command line or in phpMyAdmin, etc to see if it does actually find a result.
Another obvious one to check, but I made this error before and it took me a while to figure it. Make sure you're hashing the password stored in the database. Once I had a plain text password stored and I was hashing the password for use in the SQL query and I was wondering why it wasn't working - duh!
Best,
R.
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
•
•
•
•
Whilst developing a new script, I always find it helpful to have PHP error messages turned on. The setting for this can be found in the php.ini file on your server/computer.
I tried doing the or mysql_die() thing, but of course it says that there is no die function available.
Any other ideas?
•
•
Join Date: Jan 2008
Posts: 136
Reputation:
Solved Threads: 19
Hi,
To use PHP errors, you can also call this function:
And the 'die' command is literally just
So, if you add this to the top of your php file:
and after you've constructed your mysql query and before you execute it, call:
Then if you can post the output and try running the query in phpMyAdmin or on the MySQL command line.
R
To use PHP errors, you can also call this function:
ini_set("display_errors", 1);
ini_set('error_reporting',E_ALL);And the 'die' command is literally just
die; . Don't add the mysql_die beforehand.So, if you add this to the top of your php file:
ini_set("display_errors", 1);
ini_set('error_reporting',E_ALL);and after you've constructed your mysql query and before you execute it, call:
echo $query; die;
Then if you can post the output and try running the query in phpMyAdmin or on the MySQL command line.
R
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
The MySQL query is not working, it seems like it would work though;
But it is not getting anything, it it because i've encrypted it into the SHA thing, and it can't search for that, its just that loads of people in my computing class at school, have got it working, with this same code.
It looks like it's running the query, because the
Is just displaying the actual query
SQL Syntax (Toggle Plain Text)
SELECT * FROM members WHERE email = 'test@test.com' AND password = SHA('test')
But it is not getting anything, it it because i've encrypted it into the SHA thing, and it can't search for that, its just that loads of people in my computing class at school, have got it working, with this same code.
It looks like it's running the query, because the
PHP Syntax (Toggle Plain Text)
echo $query; or die;
Is just displaying the actual query
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
Ah i figured it out!
Pretty simple when i look at it, there was nothing wrng with my code, the size of the password field in the database was too small.
Obviously the SHA() function creates the encryption up to more than 20 characters long, and i had set my MySQL database to save only 20 of those characters... haha oops.
So when you came back to SELECT * FROM members WHERE password = SHA('test'), that was trying to find a password field with over 20 characters long, and none existed.
Anyway, there was nothing wrong with my code, just the actual database.
Cheers for all your help people.
Pretty simple when i look at it, there was nothing wrng with my code, the size of the password field in the database was too small.
Obviously the SHA() function creates the encryption up to more than 20 characters long, and i had set my MySQL database to save only 20 of those characters... haha oops.
So when you came back to SELECT * FROM members WHERE password = SHA('test'), that was trying to find a password field with over 20 characters long, and none existed.
Anyway, there was nothing wrong with my code, just the actual database.
Cheers for all your help people.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Writing to a file
- Next Thread: php query headers
| Thread Tools | Search this Thread |
apache api array basic beginner broken cache cakephp class cms code computing confirm countingeverycharactersfromastring cron curl customizableitems database date delete dynamic echo email error fcc file filter folder form forms forum freelancing function functions gc_maxlifetime google header headmethod howtowriteathesis href htaccess html iframe image include incode ip javascript joomla limit link login malfunction match memmory memory menu method mod_rewrite multiple mysql navigation neutrality oop pagerank parsing paypal pdf php phpmysql query question random recursiveloop root script search select server sessions sms snippet soap source space sql support! system table template thesishelp trouble tutorial upload url variable video web window.onbeforeunload=closeme; youtube





