Hi,
I'm trying to create a little web page but when i try to log in using the username and password i put in MySQL database it doesn't do anything at all.which is when i click login it just goes blank. I don't know why this is. Could you please take a look at my login.php and login_check.php code that i have attached to this mail to see if I did anything wrong or I didn't put something. I'm using php and MySQL. Thank you
dami06 0 Junior Poster in Training
<?php
require_once("include/inc_global.php");
// --------------------------------------------------------------------------------
// Process GET/POST
$msg = fetch_GET('msg', null);
switch ($msg) {
case 'connfailed' :
$message_class = 'warning';
$message = 'A connection to the authentication server could not be established.<br />Please try again later.';
break;
// --------------------
case 'denied' :
$message_class = 'warning';
$message = 'You attempted to access a restricted page.<br />It may be that your session has timed out so please re-enter your details.';
break;
// --------------------
case 'invalid' :
$message_class = 'warning';
$message = 'Your username and password were rejected.<br />Please check your details and try again.';
break;
// --------------------
case 'logout' :
$message_class = 'info';
$message = 'You have logged out.<br />If you wish to log back in, please re-enter your details.';
break;
// --------------------
default :
$message_class = 'info';
$message = 'To start using CAPAT you have to log in.';
break;
}
// --------------------------------------------------------------------------------
// Begin Page
$UI->page_title = 'CAPAT Login';
$UI->menu_selected = '';
$UI->breadcrumbs = array (
'login page' => null ,
);
$UI->head();
?>
<style type="text/css">
<!--
p.warning { color: #f00; }
p.info { color: #000; }
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
var username_focussed = false;
var password_focussed = false;
function username_focus() {
if ( (!username_focussed) && (!password_focussed) ) { document.getElementById('username').focus(); }
}
//-->
</script>
<?php
$UI->body('onload="username_focus();"');
$UI->content_start();
?>
<?php echo("<p class=\"$message_class\">$message</p>"); ?>
<div class="content_box">
<p>Please enter your details below:</p>
<form action="login_check.php" method="post" name="login_form" style="margin-bottom: 2em;">
<div style="width: 300px;">
<table class="form" cellpadding="2" cellspacing="1" width="100%">
<tr>
<th><label for="username">Username</label></th>
<td><input type="text" name="username" id="username" maxlength="30" size="10" value="" onfocus="username_focussed=true" onblur="username_focussed=false" /></td>
</tr>
<tr>
<th><label for="password">Password</label></th>
<td><input type="password" name="password" id="password" maxlength="16" size="10" value="" onfocus="password_focussed=true" onblur="password_focussed=false" /></td>
</tr>
</table>
<div class="form_button_bar">
<input class="safe_button" type="submit" name="submit" value="login" />
</div>
</div>
</form>
<p>This site requires cookies - If you have trouble logging in, please check your cookie settings.</p>
</div>
<?php
$UI->content_end(false);
?>
<?php
require_once("./include/inc_global.php");
//only load the authentication class we actually need
if (AUTH__CLASS == 'LDAPAuthenticator'){
require_once(DOC__ROOT.'/library/classes/class_ldap_authenticator.php');
}elseif (AUTH__CLASS == 'DBAuthentication'){
require_once(DOC__ROOT.'/library/classes/class_db_authenticator.php');
}
require_once(DOC__ROOT.'/library/functions/lib_string_functions.php');
// --------------------------------------------------------------------------------
// Process Get/Post
$username = (string) fetch_POST('username', null);
$password = (string) fetch_POST('password', null);
// Sanitize the username/password data
$username = substr($username,0,32);
$password = substr($password,0,32);
// --------------------------------------------------------------------------------
// Attempt Login
$auth_success = false;
if ( ($username) && ($password) ) {
$authenticated = false;
// Authenticate...
$_auth = new Authenticator($username, $password);
$authenticated = $_auth->authenticate();
// if authentication successful..
if ($authenticated) {
// We need to get the user_id of the person authenticated
$_user_id = null;
// Use the email address to look them up...
if ($_auth->email) {
$user_info = $CIS->get_user_for_email($_auth->email);
if ($user_info) {
$_user_id = $user_info['user_id'];
$_user_admin = $user_info['admin'];
}
}
// Save session data
$_SESSION['_user_id'] = $_user_id;
$_SESSION['_admin'] = $_user_admin;
// Save cookie data
$_cookie->vars['user_id'] = $_user_id;
$_cookie->vars['admin'] = $_user_admin;
$_cookie->save();
header('Location: '.APP__WWW.'/index.php?id='.$_user_id); // This doesn't log them in, the user_id just shows as a debug check
exit;
}
// If we got this far, authorisation failed - set the msg and return to the login screen
$msg = $_auth->get_error();
} else {
$msg = 'invalid';
}
header('Location: '.APP__WWW.'/login.php?msg='. $msg);
exit;
?>
nav33n 472 Purple hazed! Team Colleague Featured Poster
Since you are using classes in your script, its kinda difficult to make out what it does. (fetch_GET, fetch_POST, authenticator, etc. ). The best way to debug your code would be to print or echo messages to check the flow of your script. You can use print_r($_POST) in login_check.php to see if all the variables are posted properly. You can echo your sql statements( i dont see any!) and execute it to see if it returns a resultset. You can use die function to get mysql error message. And also, check for 'unwanted' exit keyword.
Cheers,
Naveen
dami06 0 Junior Poster in Training
hi,
to be honest. I'm a beginner at php and someone actually helped me out with that code (wrote it for me, but the person is unavailable now) . As someone who doesn't really know programming, what do you think i can do with the code for it to work. Is it possible to tell me what to type down in my code.. Any help would be really appreciated. thank you.
nav33n 472 Purple hazed! Team Colleague Featured Poster
hmm.. Since you are a beginner, I dont think you will understand OOPS concepts if you have no idea about it. You can either start learning OOPS concepts like classes, objects, then make your code work OR wait for the one who helped you create this code to make it work OR create a new login page from the scratch.
dami06 0 Junior Poster in Training
and if i may add. when i type in the username and password and click on login it. It takes me to localhost/capat/login_check.php and then just produces this
?>
does this help in anyway?
dami06 0 Junior Poster in Training
ok may be i'll try and create a new login in page from scratch.thank you for your time
nav33n 472 Purple hazed! Team Colleague Featured Poster
and if i may add. when i type in the username and password and click on login it. It takes me to localhost/capat/login_check.php and then just produces this
?>
does this help in anyway?
?> ? I guess you have php ending tags somewhere in your html. Anyways, All the best..
Walkere 19 Junior Poster in Training
ok may be i'll try and create a new login in page from scratch.thank you for your time
That would be your best bet. It's particularly hard to help someone that doesn't understand the code at all - so I would always suggest learning the basics before you try and implement someone else's script.
If you're looking for a resource to learn php, check out Practical PHP Programming. It's a great, free book - the same one I used to learn a while back.
Good luck,
- Walkere
dami06 0 Junior Poster in Training
thanks a lot walkere, im checking the the link out now
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.