Jump to content
Sign in to follow this  
martind

session after ajax login

Recommended Posts

hi,

i have a login form on my page which is handled within a template called by ajax. Login works fine, but only if the site doesn't get reloaded - which isn't the intention but may occur. So is there a way to manually reset the session data from within my form-handler template after successfully calling $session->login($username, $password), to have it working after a page reload?

thanks, martin

Share this post


Link to post
Share on other sites

to further describe this a bit.

home.php

$form_login = "<form id='loginForm' class='uk-form' action='/proc/login/' method='post'>\n";
$form_login .= "<fieldset data-uk-margin>\n";
$form_login .= "<legend>Login</legend>
	<input type='text' name='login_name' placeholder='E-Mail'>
	<input type='text' name='password' placeholder='password'>
	<input type='submit' class='uk-button' value='Login' />\n";
$form_login .= "</fieldset>\n";
$form_login .= "</form>\n";

xx.js

$('#loginForm').ajaxForm({
  dataType:		'json',
  success:		processFormSuccess
});
function processFormSuccess(data) {
  // 'data' is the json object returned from ajax.php
  if(data.statusCode == 2)
  {
    $('#login_content').html(data.responseText);
    $('#reg_login').html(data.username);
    $('.hide_loggedin').hide();
    $('.show_loggedin').show();
  }else{
    $('#reg_messages').html(data.responseText).fadeIn('fast');
  }
}

ajax.php

if($input->urlSegment1 == "login"){
  $r = array(
  "username" => "",
  "userid" => "",
  "email" => "",
  "responseText" => "",
  "einreichCode" => "",
  "statusCode" => 0
  );

if ($input->post->login_name && $input->post->password) {

  $username = $input->post->login_name;
  $password = $input->post->password;				

  // find user with email
  $ufind  = $pages->get('template=user, email='.$sanitizer->email($username));
  if($ufind->id)
  {
  try{
      $u = $session->login($ufind->name, $password);
      if ($u->id) {
        $log->save('login', "Login by {$u->screen_name} ({$u->id}).");
        $r['responseText'] .= "<p>welcomoe back, {$u->screen_name}.</p>";
        $r['einreichCode'] = $password;
        $r['email'] = $u->email;
        $r['userid'] = $u->id;
        $r['username'] = $u->screen_name;
        $r['statusCode'] = 2;       

      } else {
        $log->save('login', "Login fault");
        $r['responseText'] .= "<p>Falsche E-Mail <strong>>>{$username}<<</strong> oder Einreich-ID <strong>>>{$password}<<</strong>. Login fehlgeschlagen.<br >
          Versuchen Sie es erneut oder kontaktieren Sie uns unter <a href='mailto:admin@xx.net'>admin@xx.net</a></p>";
      }
    } catch(Exception $e){ $log->save('login', $e->getMessage());  }
  }
}else{
	$log->save('login', "Fehler. Keine Login Daten.");
}
$response = json_encode($r);
echo $response;			
}

 

... so, everything of this works as expected, the js function switches my home-site to logged-In mode, even when i directly call my pw-admin, the user is logged-in, but when reloading the home-site, it is not. Any way to effect this?

thanks,
martin

Share this post


Link to post
Share on other sites

It seems like you need to handle the "online" mode on your home.php template. You only update the content after submitting the form, if you directly visit the homepage, you will still see the login form even though you have a valid session. So you need to differentiate two cases in home.php:

  1. Not logged in: Display Login form
  2. Already logged in: Display "online" mode, hiding login etc. Basically the same as you're doing in javascript after successful login

Cheers

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
      //edit: I've noticed something interesting; despite the directories of my web folders being the same layout; when I open the admin page i get a 404 on the processwire/ resource in the networks panel of chrome; on my laptop I get a  200.... I guess this is where my problem is; but why?
       
       
    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
       
    • By Peter Knight
      How do you guys handle large session tables when sessions are being recorded to the database?
      I notice one of my sites has a session table of over 14MB 
      Am I missing a way in the Admin or a module to auto-remove any sessions older than X days?
      Thanks
       
    • By helmut2509
      In my PW-Application there is currently no session timeout.
      I want to set the user session to 60 minutes which means that after 60 minutes of inactivity the user will be redirected to the homepage.
      so I added the following entry to my config.php:
      $config->sessionExpireSeconds = 120; (120 seconds is just for testing).
      But after five minutes of inactivity I am still logged in, there is no redirection.
      Is there anything wrong or did I miss something?
      In php.ini I have the entry:
      session.cookie_lifetime = 3600
    • By celfred
      Hello,
      I'm facing a weird issue here. I have a page loaded with this code inside (my comments in line ends) :

      if ($session->allPlayers) { // Set in a head.inc file. I have also a $session->set('allTeams', $allTeams); in my head.inc   $allPlayers = $session->allPlayers; } else {   $allPlayers = getAllPlayers($user, false);   $session->set('allPlayers', $allPlayers); } bd($session->getAll()); // HERE, I get a number of 11 variables which is what I expect In the same page, I have a link pointing to ajaxContent.php that loads stuff via Ajax.
      I just write this in my ajaxContent.php to test :

      bd($session->getAll()); // HERE, I get only 9 variables. All my newly set $session variables ($allTeams and $allPlayers) are not conveyed to ajaxContent.php ??? Would you have any idea why is that ??? Another thing : I have a $session->headMenu set in my head.inc, and this one works fine. I can retrieve it in my ajaxContent.php page.
      I've tried cleaning all caches but it doesn't change anything 😞 
      At first, I expected it to be a 15-minute update to my site... It turns out to be a 2-hour issue and I'm still  stuck.
      Thanks for your ideas ! 
×
×
  • Create New...