Jump to content

Login Authentication


mister X

Recommended Posts

I'm going to assume the following: the passwords in your database table are hashed (that is, you're not storing the actual passsword in plaintext).

 

<?php
if(isset($_POST) && !empty($_POST)){//check to see if they submitted the form
$username = mysql_real_escape_string($_POST['username']);//escape username
$password = md5(mysql_real_escape_string($_POST['password']));//escape and hash
$result = @mysql_fetch_array(@mysql_query("SELECT `password` FROM `users` WHERE `username` = '$username' LIMIT 1"));
if($password == $result){//the two match
echo "Access Granted";
//do stuff
} else {
echo "Access Denied";
//do other stuff
}
}
?>

Meh, if you can't get this part down, I suggest googling some tutorials for a log in system and trying to understand how it comes together.

Link to comment
Share on other sites

Hey, did try out but somehow, it cant seem to proceed on.. I get the page loaded up. But after I enter in my username and password. It just doesnt redirect me to index page.

 

<?php

// we must  start the session

session_start();

 

$_SESSION['flag'] = true;

 

if (isset($_POST['txtUserId']) && isset($_POST['txtPassword']))

{

// store the user's input in the variables

$userid = $_POST['txtUserId'];

$password = $_POST['txtPassword'];

 

// connect to the lcoalhost and select the appropriate database

$sql_index = mysql_connect('localhost', 'root', 'password');

$db_select = mysql_select_db('network_deployment_system', $sql_index);

 

echo $db_select;

 

// retrieve the username and password base on the user's input

$sql_query = mysql_query('select * from authorised_user where UserID=\''.$userid.'\' and Password=\''.$password.'\'');

 

// check whether there is a match

if (mysql_num_rows($sql_query) == 1)

{

// fetch data from the table

$data = mysql_fetch_row($sql_query);

// store the data into session variables

$_SESSION['UserID'] = $data[0];

$_SESSION['Password'] = $data[1];

$_SESSION['Administrator'] = $data[2];

// response and redirect to index page once login successful

header('Location: Index.php');

}

else

{

$_SESSION['flag'] = false;

}

 

}

?>

Link to comment
Share on other sites

Adding on, whenever I enter in my password and UserID, and click on login, they will redirect to a blank page. Isit a case of wrong coding? pls advice. Thanks

 

Add error reporting to the top of the script and see what it says.

 

error_reporting(E_ALL);
ini_set("display_errors", 1);

Link to comment
Share on other sites

hi mister x,

 

Try chaging:

$sql_query = mysql_query('select * from authorised_user where UserID=\''.$userid.'\' and Password=\''.$password.'\'');
// check whether there is a match
if (mysql_num_rows($sql_query) == 1)

 

to:

$sql = mysql_query('select UserID, Password from authorised_user where UserID='$userid' and Password='$password');
// check whether there is a match
if (mysql_num_rows($sql) == 1)

Link to comment
Share on other sites

Also, I notice your line:

 

header('Location: Index.php');

 

I assume this file is stored on your server as "Index.php" and not "index.php".  A lot of servers use case sensitive URL's so make sure you use the right case for the "i" at the beginning of "Index.php".

Link to comment
Share on other sites

Hmm.. I got the following error msg after i enter in my userid and password.

 

Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\terminalserver-soft\Login.php on line 17

 

What should I do now then?

Link to comment
Share on other sites

Hmm.. I got the following error msg after i enter in my userid and password.

 

Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\terminalserver-soft\Login.php on line 17

 

What should I do now then?

 

That means you cannot connect to your Database. Is your information correct?

Link to comment
Share on other sites

No that means that php is enabled on his server but MySQL is not.

 

If you have access to php.ini uncomment the line that says

 

extension=php_mysql.dll

 

On windows. I'm not sure what the exact name is on linux based systems... but basically search through the file for "mysql" and you should find it under the extension section of the ini.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.