Jump to content

Login does not log in always


Harmster
 Share

Recommended Posts

Hey PW people!

I made a custom login form which sends a request to an AJAX page that should log me in, I'll post the scripts in a second. But I'd get a weird error of some sort.

This is what I do

I log in on my login form (/user/login)

It logs me in, session is created and everything

I go to the homepage -> i log out -> i go back to the login page and i login again, this is where i can't login i get a message "Incorrect password and username"(my custom message which is returned at a NULL return which means it fails right?!

So these are my scripts

Login.php:

<?php
if(!$user->isLoggedin())
{
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $("#login").click(function(){
	 $("#ajax").html("Laden...");
	 $.ajax({
		 url: '/site/ajax/ajaxResponder.php',
		 type: 'post',
		 dataType: 'JSON',
		 data: {
			 'action': 'login',
			 'username': $("#username").val(),
			 'password': $("#password").val()
		 },
		 success:function(data){
			 alert(data.pass);
			 if(data.status)
			 {
				 window.location = "/";
			 }
			 $("#ajax").html(data.message);
		 }
	 });
		 return false;
 });

});
</script>
<?php

?>
<form action ="" method="post">
 <label for="username">Gebruikersnaam:</label>
 <input type="text" name="username" id="username" />
 <label for="password">Wachtwoord:</label>
 <input type="password" name="password" id="password" />
 <input type="submit" value="Login" id="login"/>
</form>
<div id="ajax"></div>
<?php
}
else
{
echo 'Je bent ingelogt.<br /><a href="/user/logout">Log uit</a>';
}
?>

This is ajaxResponder (the part that matters, the case in a switch $_POST['action'])(/site/ajax/ajaxResponder.php)

case 'login':

		 $pass = $_POST['password'];
		 $user = $_POST['username'];
		 $r = wire('session')->login($user, $pass);
		 if($r)
		 {
			 $re['message'] = 'U bent ingelogd';
			 $re['status'] = true;
		 }
		 else
		 {
			 $re['message'] = 'Uw gebruikersnaam of wachtwoord is onjuist';
			 $re['status'] = false;
		 }
		 echo json_encode($re);
	 break;

So what can I do to fix this problem?

Kind regards,

Harm.

Link to comment
Share on other sites

Hard to tell without having the full thing in front of me to step through live. But I'd suggest performing a redirect after a login or logout, so that you are starting with a fresh request, rather than a page pre-login with login happening behind the scenes. Seems like there is possibility for some session conflicts there, though again not positive without experimenting on the live code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...