Page 1 of 1

Login Function That compares The Username And Password From

Posted: Tue May 03, 2005 3:44 pm
by Arsenal Rule
Hey I created this code for a login function

Code: Select all

<?
	$uname = isset($_POST['uname'])?($_POST['uname']): "";
	$pass = isset($_POST['pass'])?($_POST['pass']): "";
	if (isset($_POST['checklog']) == true):
		$validate = 0;
		if ($uname == 'test')  {$validate++; }
		if ($pass == 'test') {$validate++; }
		if ($validate == 2) 
		{
			session_start();
			$_SESSION['_suname'] = $uname;
			$_SESSION['_spass'] = $pass;
			 header("Location: mainmenu.php?" .SID); }
		else { print "<I> Password or username error </I>"; }
	endif;
?>
<HTML><HEAD><TITLE> Temp Logon page </TITLE></HEAD>
<BODY>
<H2> Logon Here: </H2><BR>
<FORM name='templogon' method='POST'  action='templogon.php'>
Username: <INPUT type='text' name='uname'><BR>
Password:  <INPUT type='password' name='pass'><BR>
<INPUT TYPE='submit' name='checklog' value='submit'>
</FORM></BODY></HTML>
This code uses the login features and compares the username and passwords that is set with in the code.
What I would like to know is how I can convert this code into a login code that will be able to compare the fields "passwords" and "username" from a password table "azizpassword".

And when it doesn't match what is typed in it will bring out an error message but if it does match it will go to and open the file mainmenu.php.

Basically I just want to know how I can convert the code given above to what I need and explain.

I really hope someone can help me coz I hae been trying different ways but none of them have worked and because of me being a beginner at PHP I find it very difficult to solve this problem I hope someone can help me with this

Thank You For Your Time

Take Care

Posted: Tue May 03, 2005 4:26 pm
by Calimero
first of all - search these forums,


If you want connection to the Database ( and you are using MySQL ) here is the code:

Code: Select all

// Check for input parameters
$user = ( isset($_POST['uname']) ) ? $_POST['uname'] : "ERROR";
$pass = ( isset($_POST['pass']) ) ? $_POST['pass'] : "ERROR";

// Check if both parameters are entered, if yes proceed, else go back
if ( $user == "ERROR" || $pass == "ERROR" )
{
echo "Please enter both your Usename and Password";
}
else
{
// Connection to mysql server ( explained below )
mysql_connect('your_host','your_username','your_pass') or die ('Error with the connection');

// Selecting your database
mysql_select_db('name_of_the_db') or die ('Error with the Database');

// MySQL query
$qry = mysql_query("SELECT user FROM azizpassword a WHERE a.user = '".$user."' AND a.pass = '". $pass ."';");

// Checking if the username and password exist in the table and if they are correct
if ( mysql_num_rows($qry) == 1 )
{
// If yes - go here
header("Location: mainmenu.php");
}
else
{
// else - go back.
echo "Please verify again";
}
}
This requires you to have a MySQL server and your user and pass for it, one DB to hold the table in it, and columns in that table to store the data ( I proposed 2 columns - user and pass in the table azizpassword )

Login Function

Posted: Tue May 03, 2005 4:53 pm
by Arsenal Rule
Thank you so much mate for replying I just want to know I know this might sound stupid but its coz my imexperience with php I just want to know where I can place the code you showed me in my existing code to allow it to work efficiently.
Or is thcode you gave me the full thing already

Please can you help me mate to solve this problem.

Thank you for your help I really appreciate mate take care :D

...

Posted: Tue May 03, 2005 5:08 pm
by Calimero
This code takes care of DB conectivity and data retrieval, I didn't implement SESSIONS as you did - so thats your part of the job.

Also - because you said that you are the begginer in the PHP - I don't know your knowledge in MySQL.

Have you installed the server, do you have any software to manage your Database, and do you know how to use MySQL - mainly to create tables and their columns.

If you don't have mysql - search for it in google, and get further information from their site.

Hope this helped.

Login Function

Posted: Tue May 03, 2005 6:36 pm
by Arsenal Rule
Thanx alot mate for you reply yes it has helped alot thankyou very much I really appreciate your help thank you. I am using and MySql database I'm just getting the hang of using it. Thank You So Much for your Help Again

Take Care :D