/    Sign up×
Community /Pin to ProfileBookmark

Login script – Not doing its job!

Hi there,

[url]www.loddonexplorers.co.uk/RegLogin.php[/url]

Dosent seem to want to work! Any user/password combo i type in works fine!
Any ideas why as im stumped!

Cheers,
Fet

[code=php]
<?php
session_start();
if (array_key_exists(‘Username’, $_SESSION)) {
process_form(1);
}
else {
if ($_POST[‘_submit_check’]) {
if ($form_errors = validate_form()) {
show_form($form_errors);
}
else {
process_form(0);
}
}
else {
show_form();
}
}

function show_form($errors = ”) {
print ‘<form name=”authForm” method=”POST” action=”‘.$_SERVER[‘PHP_SELF’].'”>’;
if ($errors) {
print ‘<span style=”color:red”><ul><li><b>’;
print implode(‘</b></li><li><b>’,$errors);
print ‘</b></li></ul></span>’;
}
print ‘Username’;
print ‘<input type=”text” name=”Username” value=”‘;
print htmlentities($_POST[Username]) . ‘”> <br />’;
print ‘Password’;
print ‘<input type=”password” name=”Password” value=”‘;
print htmlentities($_POST[Password]) . ‘”> <br />’;

print ‘<input type=”submit” name=”login” value=”Login” />’;
print ‘<input type=”hidden” name=”_submit_check” value=”1″/>’;
print ‘</form>’;
print ‘<a href=”index.php”>Click</a> here if you want to leave this form.’;
}

function validate_form() {
$errors = array();
$UserID=$_POST[‘Username’];
$UserPass=$_POST[‘Password’];
/****************************************************************************
* Check username and password in database *
****************************************************************************/
$userconn = @mysql_connect(“**”, “**”, “**”) or
die(“ERROR: Unable to establish database connection”);

$dbconn = @mysql_select_db(“**”) or
die( “Unable to select database”);

$text = “”;
$text = @mysql_query(“SELECT * FROM tblExplorerGroup WHERE Username=’$UserID’ AND Password=sha1(‘$UserPass’)”) ;
if ($text == “”) {
$errors[] = ‘Enter a valid username and password!’;
}
return $errors;
}

function process_form($logged_in) {
if ($logged_in == 0) {
// Add the username to the session
$_SESSION[‘Username’] = $_POST[‘Username’];
}
print ‘You are logged in as: <b>’.$_SESSION[‘Username’].'</b>’.str_repeat(‘&nbsp;’, 10).'<a href=”RegLogout.php”>Logout</a><br />’;
print ‘<p>You can continue processing as a logged-in user …………</p>’;
}
?>
[/code]

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Dec 29.2006 — The problem is in your [B]validate_form()[/B] function. In it, the condition [B]$text == ""[/B] will never happen because the result of the [B]mysql_query()[/B] will never just return an empty string.

It will either return [B]FALSE[/B] or a recordset resource.

As a minimum, you want to check if the query returned 1 or 0 rows, using [B]mysql_num_rows()[/B]
Copy linkTweet thisAlerts:
@scottyrobauthorDec 29.2006 — So im going to want to change this line...

if ($text == "") {

So that it checks the form field Username against the DB item UserID and the form field Password against UserPass?

Ill have a go, no idea how (Someone else showed me this script), but i could make some guesses

Fet

EDIT:

Well ive had a go and come up with this.. It would only validate the user name in theroy... Dosent work though ?

[code=php]
$text = "";
$text = @mysql_query("SELECT * FROM tblExplorerGroup WHERE Username='$UserID' AND Password=sha1('$UserPass')") ;
$row = mysql_fetch_assoc($text);
$DBUserID = $row['Username'];
if ($UserID == $DBUserID) {
$errors[] = 'Enter a valid username and password!';
}
return $errors;
}

[/code]


2nd Edit..

Acctually that would Invalidate the user if they entered the correct username would it?

Scott
Copy linkTweet thisAlerts:
@NightShift58Dec 29.2006 — Your function needs/can look something like this:[code=php]function validate_form() {

$userconn = @mysql_connect("**", "**", "**") or die("ERROR: Unable to establish database connection");
$dbconn = @mysql_select_db("**") or die( "Unable to select database");

$UserID = $_POST['Username'];
$UserPass = $_POST['Password'];

$sql = "SELECT * FROM tblExplorerGroup WHERE Username='$UserID' AND Password=sha1('$UserPass') LIMIT 1";
$qry = @mysql_query($sql);

$returnCODE = FALSE;
IF ($qry) :
IF (mysql_num_rows($qry) == 1) :
$returnCODE = TRUE;
ENDIF;
ENDIF;

return $returnCODE;
}[/code]
If the query "worked" and returned one row, there's no need to check for matching UserID since that was precisely the subject of your query.

Note that I added "LIMIT 1" to your query, as it's fairly safe to assume that only one such record will/should exist and we save MySQL from having to look beyond the first match.

I changed the return value in your function to just TRUE/FALSE. Instead of passing arrays back and forth, you can assign whatever message value in he main script. You can change that but it probably makes more sense to just have a TRUE/FALSE condition at the end of the function as it will make your function more portable without having to change the message part.

This means that this part will have to be changed from:[code=php]<?php
session_start();
if (array_key_exists('Username', $_SESSION)) {
process_form(1);
} else {
if ($_POST['_submit_check']) {
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form(0);
}

} else {
show_form();
}
}[/code]
to something like:[code=php]<?php
session_start();
if (array_key_exists('Username', $_SESSION)) {
process_form(1);
} else {
if ($_POST['_submit_check']) {
if (!validate_form()) {
show_form('Enter a valid username and password!');
} else {
process_form(0);
}

} else {
show_form();
}
}[/code]
Copy linkTweet thisAlerts:
@scottyrobauthorDec 30.2006 — Hi there,

Thanks for that code & help. Im getting an error when running the below code...

Warning: implode(): Bad arguments. in /home/loddouk1/public_html/RegLogin.php on line 21

The website is above as you know!

Cheers,

Fet

[code=php]
<?php
session_start();
if (array_key_exists('Username', $_SESSION)) {
process_form(1);
} else {
if ($_POST['_submit_check']) {
if (!validate_form()) {
show_form('Enter a valid username and password!');
} else {
process_form(0);
}

} else {
show_form();
}
}

function show_form($errors = '') {
print '<form name="authForm" method="POST" action="'.$_SERVER['PHP_SELF'].'">';
if ($errors) {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}

print 'Username';
print '<input type="text" name="Username" value="';
print htmlentities($_POST[Username]) . '"> <br />';
print 'Password';

print '<input type="password" name="Password" value="';
print htmlentities($_POST[Password]) . '"> <br />';

print '<input type="submit" name="login" value="Login" />';
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
print '<a href="index.php">Click</a> here if you want to leave this form.';
}

function validate_form() {

$userconn = @mysql_connect("..", "..", "..") or die("ERROR: Unable to establish database connection");
$dbconn = @mysql_select_db("..") or die( "Unable to select database");

$UserID = $_POST['Username'];
$UserPass = $_POST['Password'];

$sql = "SELECT * FROM tblExplorerGroup WHERE Username='$UserID' AND Password=sha1('$UserPass') LIMIT 1";
$qry = @mysql_query($sql);

$returnCODE = FALSE;
IF ($qry) :
IF (mysql_num_rows($qry) == 1) :
$returnCODE = TRUE;
ENDIF;
ENDIF;

return $returnCODE;
}
function process_form($logged_in) {
if ($logged_in == 0) {
// Add the username to the session
$_SESSION['Username'] = $_POST['Username'];
}
print 'You are logged in as: <b>'.$_SESSION['Username'].'</b>'.str_repeat('&nbsp;', 10).'<a href="RegLogout.php">Logout</a><br />';
print '<p>You can continue processing as a logged-in user ............</p>';
}
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Dec 30.2006 — That particular line expects "$errors" to be an array.

You either rethink your script and decide not to use arrays as a holder for your error message, in which case you'll no longer need to "implode()", or at the top of the function, you can check if the incoming parameter is an array of not. Something like this:[code=php]function show_form($errors = '') {
IF (!is_array($errors)) :
$errors = array($errors);
ENDIF;
...[/code]
This will convert "$errors" into an array if and when it isn't one to begin with. The rest of your script should continue working after that.
Copy linkTweet thisAlerts:
@scottyrobauthorDec 30.2006 — Hi there

Um, It dosent want to validate anything now!

All ive changed from the above code is this section...

Cheers,

Scott

[code=php]
function show_form($errors = '') {
IF (!is_array($errors)) :
$errors = array($errors);
ENDIF;
print '<form name="authForm" method="POST" action="'.$_SERVER['PHP_SELF'].'">';
if ($errors) {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}

[/code]
Copy linkTweet thisAlerts:
@NightShift58Dec 30.2006 — This part of the script doesn't really have any influence on user login validation. Can you post the entire script as it is now?
Copy linkTweet thisAlerts:
@scottyrobauthorDec 30.2006 — Thanks very much for the help! I really appreciate this!

Cheers,

Scott

[code=php]
<?php
session_start();
if (array_key_exists('Username', $_SESSION)) {
process_form(1);
} else {
if ($_POST['_submit_check']) {
if (!validate_form()) {
show_form('Enter a valid username and password!');
} else {
process_form(0);
}

} else {
show_form();
}
}

function show_form($errors = '') {
IF (!is_array($errors)) :
$errors = array($errors);
ENDIF;
print '<form name="authForm" method="POST" action="'.$_SERVER['PHP_SELF'].'">';
if ($errors) {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}

print 'Username';
print '<input type="text" name="Username" value="';
print htmlentities($_POST[Username]) . '"> <br />';
print 'Password';

print '<input type="password" name="Password" value="';
print htmlentities($_POST[Password]) . '"> <br />';

print '<input type="submit" name="login" value="Login" />';
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
print '<a href="index.php">Click</a> here if you want to leave this form.';
}

function validate_form() {

$userconn = @mysql_connect("l.", "l.n", "m.") or die("ERROR: Unable to establish database connection");
$dbconn = @mysql_select_db("l.n") or die( "Unable to select database");

$UserID = $_POST['Username'];
$UserPass = $_POST['Password'];

$sql = "SELECT * FROM tblExplorerGroup WHERE Username='$UserID' AND Password=sha1('$UserPass') LIMIT 1";
$qry = @mysql_query($sql);

$returnCODE = FALSE;
IF ($qry) :
IF (mysql_num_rows($qry) == 1) :
$returnCODE = TRUE;
ENDIF;
ENDIF;

return $returnCODE;
}
function process_form($logged_in) {
if ($logged_in == 0) {
// Add the username to the session
$_SESSION['Username'] = $_POST['Username'];
}
print 'You are logged in as: <b>'.$_SESSION['Username'].'</b>'.str_repeat('&nbsp;', 10).'<a href="RegLogout.php">Logout</a><br />';
print '<p>You can continue processing as a logged-in user ............</p>';
}
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Dec 31.2006 — I've made some "cosmetic" changes to the script, trying to tighten it up a bit and one structural change in the initial script flow.

I found no place in the script which would lead to the error you are now reporting. I say this assuming that you didn't forget to put the database login information back in the appropriate places in the script.

The only issue I have with the script is that if the session variable Username exists, you automatically let the user in. I don't know if that's good, because I don't know if you ever unset this variable. That may be something you want to double check.

Here's an updated version:[code=php]<?php
session_start();

IF (array_key_exists('Username', $_SESSION)) :
process_form();
ELSEIF ($_POST['_submit_check']) :
IF (validate_form()) :
$_SESSION['Username'] = $_POST['Username'];
process_form();
ELSE :
show_form('Enter a valid username and password!');
ENDIF;

ELSE :
show_form();
ENDIF;

function process_form() {
print 'You are logged in as: <b>'.$_SESSION['Username'].'</b>'.str_repeat('&nbsp;', 10).'<a href="RegLogout.php">Logout</a><br />';
print '<p>You can continue processing as a logged-in user ............</p>';
}

function show_form($errors = '') {
IF (!is_array($errors)) :
$errors = array($errors);
ENDIF;
IF (count($errors) > 0) :
print '<span style="color:red"><ul>';
FOREACH ($errors as $thisERROR) :
print '<li><b>' . $thisERROR . '</b></li>';
ENDFOREACH;
print '</ul></span>';
ENDIF;

print '<form name="authForm" method="POST" action="'.$_SERVER['PHP_SELF'].'">';
print 'Username <input type="text" name="Username" value="' . htmlentities($_POST['Username']) . '"> <br />';
print 'Password <input type="password" name="Password" value="' . htmlentities($_POST['Password']) . '"> <br />';
print '<input type="submit" name="login" value="Login" />';
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
print '<a href="index.php">Click</a> here if you want to leave this form.';
}

function validate_form() {
$userconn = @mysql_connect("l.", "l.n", "m.") or die("ERROR: Unable to establish database connection");
$dbconn = @mysql_select_db("l.n") or die( "Unable to select database");

$UserID = $_POST['Username'];
$UserPass = $_POST['Password'];

$sql = "SELECT Username FROM tblExplorerGroup WHERE Username='$UserID' AND Password=sha1('$UserPass') LIMIT 1";
$qry = @mysql_query($sql);

$returnCODE = FALSE;
IF ($qry) :
IF (mysql_num_rows($qry) == 1) :
$returnCODE = TRUE;
ENDIF;
ENDIF;
return $returnCODE;
}

?>[/code]
Copy linkTweet thisAlerts:
@scottyrobauthorDec 31.2006 — Hi there,

Thanks very much for that.. still didnt work..

Username = scott

Password = password
Copy linkTweet thisAlerts:
@NightShift58Dec 31.2006 — Please make this change at the end of validate_form():[code=php] $returnCODE = FALSE;
IF ($qry) :
IF (mysql_num_rows($qry) == 1) :
$returnCODE = TRUE;
ENDIF;
ELSE :
print "Query failed: $sql"
ENDIF;[/code]
The error message delivered means that the function returned FALSE, so we'll have to see why it's doing that now, whereas it wasn't before...
Copy linkTweet thisAlerts:
@scottyrobauthorDec 31.2006 — Hi Im getting an error with those if's!

Parse error: syntax error, unexpected T_ENDIF in /home/loddouk1/public_html/RegLogin.php on line 57

Tried fiddiling around and changing them but im not exactly sure how!

Cheers,

Scott
Copy linkTweet thisAlerts:
@scottyrobauthorDec 31.2006 — Woooo Heyyy! It works... I just removed the encryption method before the Password in my SQL query as i wasnt using it and it now works!

Thanks Very much for your help!
Copy linkTweet thisAlerts:
@NightShift58Dec 31.2006 — You're welcome.

The previous error had to do with a missing semicolon. My mistake, not yours.
×

Success!

Help @scottyrob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...