8 Replies - 44322 Views - Last Post: 13 June 2011 - 09:22 PM Rate Topic: -----

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

How to create a php login with data from mySQL database

Posted 13 June 2011 - 03:27 PM

Howdy. Can someone suggest a good tutorial for creating a login page? I have created the actual page for the user to input, and I created a login script, which is goingt o verify the credentials (username and password) against a mySQL database, but I am not sure where to put things.

Currently the login page has a submit button with the form action sending the login data to the php script in a differnet file. Anyway, I have never done this before and my PHP book doesn't really cover it that well, so any online tutorials you can suggest would be great.

thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to create a php login with data from mySQL database

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 03:43 PM

I just made a tutorial on that actually. http://www.dreaminco...eys-ip-address/
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 03:45 PM

There's also this one. PROTIP: Our PHP Tutorials section has a search function.
Was This Post Helpful? 0
  • +
  • -

#4 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 04:51 PM

Thank you guys very much, these references where super helpful. I have all the coding done now, but have one problem. Upon successful login, the user is not directed to the correct page. The login screen is index.php, the php code is in login.php, and the user should be directed to menu.html upon successful login. Instead, the browser goes to login.php which is nothing but php script, so a blank page appears.

The index.php form is simple, here is the code:
	<form action="login.php" method="post" enctype="application/x-www-form-urlencoded">
		<dl>
			<dt><label>Login Name: </label></dt>
			<dd><input type="text" name="username" class="input"/></dd>
			<dt><label>Password: </label></dt>
			<dd><input type="password" name="password" class="input" /></dd>
			<input type="submit" name="loginBtn" value="Enter Site" class="button" />
		</dl>
	</form>




And here is the php code from login.php:

<?php

session_start();
require_once('includes.php');


	//Connect to server
	$link = mysql_connect($host, $user, $pw) or die(mysql_error());
	//Select the database
	mysql_select_db ($db);

	// Get the login credentials from user
	$username = $_POST['username'];
	$userpassword = $_POST['password'];	
	
	// Secure the credentials
	$username = mysql_real_escape_string($_POST['username']);
	$userpassword = mysql_real_escape_string($_POST['password']);

	// Check the users input against the DB.
	$query = "SELECT * FROM user WHERE username = '$username' AND password = '$userpassword'";
	$result = mysql_query($query) or die ("Unable to verify user because " . mysql_error());
	
	$count = mysql_num_rows($result);
	
	if ($count == 1)
	{
		$_SESSION['loggedIn'] = "true";
		header("Location: menu.html");
// I also tried the whole URL here, but same result.

	}
	else
	{
		$_SESSION['loggedIn'] = "false";
		echo "<p>Login failed, username or password incorrect.</p>";
	}



?>



Any indication to where I am going wrong would be great, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 06:03 PM

Refrain from using PHP to count the amount of rows in a result. SQL has a function built in just for that purpose and also it'll clean your code up.

$query = "SELECT COUNT(`username`) AS `total` FROM `user` WHERE `username` = '$username' AND `password` = '$userpassword'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row['total'] == 1)
{
	//username and password found
}
else
{
	//username and password not found
}



Also don't use die especially on a login page. If there's an error in your connection your die function will spit out some valuable information that the users of your site shouldn't know and plus it's non-user friendly because it kills your script where ever it's called. Consider proper error handling.
Was This Post Helpful? 1
  • +
  • -

#6 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 06:54 PM

I fixed the count and I have started researching error handling. After making these changes, when I test the login page, I keep getting the "Login failed, username or password incorrect." I know the credentials are correct, becuase I put them in the DB. Any ideas?

<?php

session_start();
require_once('includes.php');


	//Connect to server
	$link = mysql_connect($host, $user, $pw) or die(mysql_error());
	//Select the database
	mysql_select_db ($db);

	// Get the login credentials from user
	$username = $_POST['username'];
	$userpassword = $_POST['password'];	
	
	// Secure the credentials
	$username = mysql_real_escape_string($_POST['username']);
	$userpassword = mysql_real_escape_string($_POST['password']);

	// Check the users input against the DB.
	$query = "SELECT * FROM user WHERE username = '$username' AND password = '$userpassword'";
	$result = mysql_query($query) or die ("Unable to verify user because " . mysql_error());
	
	$row = mysql_fetch_assoc($result);
	
	if ($row['total'] == 1)
	{
		$_SESSION['loggedIn'] = "true";
		header("Location: http://auto.courtesyvending.com/inspection/menu.html");
	}
	else
	{
		$_SESSION['loggedIn'] = "false";
		echo "<p>Login failed, username or password incorrect.</p>";
	}




?>


Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 06:59 PM

Check out the difference between my query and yours. Your query doesn't fetch a total column so therefore $row['total'] will be read as NULL or 0 thus making it appear as though the credentials are incorrect.

This post has been edited by codeprada: 13 June 2011 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 07:06 PM

Thank you. The login now succeeds, but I am back to the original problem of not being directed to the correct page.
Was This Post Helpful? 0
  • +
  • -

#9 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to create a php login with data from mySQL database

Posted 13 June 2011 - 09:22 PM

Direction takes place when you send the location header to a browser. Therefore check the necessary places where you would want to direct the user for correct header function call.

P.S. Nothing should be outputted to the browser before attempting to send a header. Echo, print, printf, var_dump....etc will all cause header() to throw a "Headers already sent" error.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1