Current location: Hot Scripts Forums » Programming Languages » PHP » Register / Login script PROBLEM


Register / Login script PROBLEM

Reply
  #1 (permalink)  
Old 05-23-08, 08:51 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Register / Login script PROBLEM

Hey Guys,

Go easy on me as I am new(ish) to PHP. I'm writing a register / login script for a CMS and the register functions as it should. The username, encrypted password, and email are stored in the database. When I run the query to select the match in the login script, I continuously get my echo statement ('Invalid login') which is set to echo only when the mysql_fetch_row variable comes up empty. Here is the script...

Code:
<?php
			
			// session start
			session_start();
			
			// includes
			include('./includes/config.php');

			if (isset($_POST['login']))
			{	
				// set up some variables to make this easier
				$username = $_POST['username'];
				$password = $_POST['password'];
				
				// let's deal with empty fields
				if (empty($username) || empty($password))
				{
					echo '<li>Please make sure to fill in all fields!</li>';
				} else
					{
						// they filled it all in and submitted
						// let's clean up the username and password
						$username = mysql_real_escape_string($_POST['username']);
						$password = md5($_POST['password']);
						
						// search for the combination
						$query = mysql_query("SELECT id FROM users WHERE username = '".$username."' AND password = '".$password."' ") or die(mysql_error());
						
						list($user_id) = mysql_fetch_row($query);
						
						if (empty($user_id))
						{
							echo '<li>Incorrect login!</li>';
						} else
							{
								// we found a match and we login them in
								// WITH a session
								$_SESSION['user_id'] = $user_id;
								
								echo '<meta http-equiv="Refresh" Content="0; URL=index.php">';	
							}
					}
			}

			?>
This is really basic. I'm going to add email validation, and only allow certain string lengths for the username and password. But, can someone help me figure out why it's not getting past the if(empty($user_id)) seeing as I fill in the correct log in information.

Also, I always have to use the meta http-equiv refresh as opposed to the header() function because it never runs. Is that dependable on anything?

Thanks,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-23-08, 09:18 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Second question first (also turning on error reporting might help with the first problem) - headers usually don't work because something is being output to the browser before the headers. The easiest way to troubleshoot this (and most other errors that php can detect) is to turn on full php error_reporting and set display_error on. On a development system, this is best done in the php.ini or a .htaccess file so that fatal parse errors are output as well. Turning on full php error reporting will also help you when learning php or anytime your are developing php code or debugging php code.

I'll assume this is on your own computer. In your php.ini set the following two values (stop and start your web server to get any changes made to php.ini to take effect) -

Code:
error_reporting  =  E_ALL | E_STRICT
display_errors = On
I included the E_STRICT setting in the above so that you will also be notified if you are using any depreciated functions that could be turned off or removed in future versions of php.

For the first problem - your query is apparently executing without error (the or die() statement is not being executed), so, echo what the actual query string is and then check directly in the database using your favorite database management tool to make sure the values match.

I recommend forming your query string in a variable so that you can echo it and then using that variable in the mysql_query() statement. Doing this will also help you as you write more sophisticated programs because you can then echo the query when an error occurs or log all queries to keep a record of who did what to your database.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-23-08, 09:22 PM
jstanden jstanden is offline
Newbie Coder
 
Join Date: Oct 2003
Location: Orange County, California
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
echo $query to the screen after you define it, and try running it against your database manually (in the console or phpMyAdmin/TOAD/etc).

Make sure you get the result you expect there first, then suspect the code.
__________________
Jeff Standen, Chief of R&D, WebGroup Media LLC.
http://www.cerb4.com/ - Cerberus Helpdesk 4.2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-23-08, 10:56 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I echoed the $query and got back a 'Resource id #4' about the 'Invalid Login' echo. I'm going to run the query in phpmyadmin and try and figure it out that way.

It's safe to say that the query is the problem seeing as the the die() doesn't run?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-23-08, 11:01 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Reread my post. In your code, the variable $query does not contain your query string. jstanden was just posting general information that was not specific to what your code is doing.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-23-08, 11:13 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
$query = "SELECT id FROM users WHERE username = '".$username."' AND password = '".$password."'";
						
$result = mysql_query($query) or die(mysql_error());
						
list($user_id) = mysql_fetch_row($result);
Is this what you were suggesting?

I echoed the $result and still got Resource Id #4

Also, with echoing $user_id....nothing echoes so it's empty and that is why the it returns the 'Invalid login'?

(Also, the database is called cms with a table called users consisting of four fields (id, username, password, and email) )

Last edited by drewby; 05-23-08 at 11:16 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 05-24-08, 03:25 PM
jstanden jstanden is offline
Newbie Coder
 
Join Date: Oct 2003
Location: Orange County, California
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry about that, I didn't mean to be as confusing as I was.

You were right to split up the $query so it contains the "SELECT ..." string and not the result of mysql_query(). You should be doing that anyway.

Now that you have $query separate from the mysql_query() function, you can "echo $query;". You don't want to "echo $result;" -- that's why you're getting "resource id #4". The result of mysql_query() is a pointer to a non-PHP resource (managed by PHP's internals and the MySQL database driver).

The goal of "echo $query;" is to see what your SQL statement looks like with $username and $password injected in the WHERE. You then want to run that SQL statement against your database to reassure yourself the appropriate record is coming up.

If it does, suspect your code.
If it doesn't, suspect your DB/query.

I hope that's more clear.
__________________
Jeff Standen, Chief of R&D, WebGroup Media LLC.
http://www.cerb4.com/ - Cerberus Helpdesk 4.2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 05-24-08, 03:52 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Jstanden! That does make sense now. I did just that and took the query that was echoed and ran it in the phpmyadmin and it returned an empty result set. But, the username/password matched up perfectly to the ones that were inserted into the database. So, as you said, that would mean there is a problem with the DB/query? Everything matches up right and I'm still not sure what I'm doing wrong. I guess I'll keep playing with it.....not sure what else to do...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 05-24-08, 05:46 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
If anyone has any ideas as to the problem / needs any specific details let me know. I really need help. I've been trying to figure it out for like 4 hours and nothing's working. The query is getting no results in the database even though it is returning the right thing...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 05-24-08, 09:50 PM
drewby drewby is offline
Newbie Coder
 
Join Date: May 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Nevermind....still working at it

Last edited by drewby; 05-24-08 at 10:10 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
most suitable php/mysql login script benalex Script Requests 0 06-15-07 01:19 AM
Problem with download script! Really need help with this! Oskare100 PHP 1 04-12-07 09:36 AM
Creating Website Script Problem Dainbramaged05 PHP 4 06-30-04 01:29 PM


All times are GMT -5. The time now is 09:04 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.