hackalive Posted December 26, 2011 Share Posted December 26, 2011 Hi guys, I am using an AJAX with PHP login, My issue is when i refresh it cancels my session! Any way to fix this? Cheers in advance. Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/ Share on other sites More sharing options...
hackalive Posted December 26, 2011 Author Share Posted December 26, 2011 I am using the code from: http://blog.webwizo.com/2011/05/04/simple-login-with-php-and-jquery-ajax/ Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301374 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2011 Share Posted December 26, 2011 The dologin.php code in that example only returns the 'success' status back to the ajax code. It would be up to you to modify your actual login code to do the same upon successfully authenticating the visitor's username/password. It would be your login code that sets up the necessary session variables to identify the current visitor and determine if he is logged in. Edit: That code you found would be better titled as - "jquery login form submission with success/failure message." Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301380 Share on other sites More sharing options...
hackalive Posted December 26, 2011 Author Share Posted December 26, 2011 Do you have a suggestion for a better example? Or able to help me out with the code please? Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301385 Share on other sites More sharing options...
hackalive Posted December 27, 2011 Author Share Posted December 27, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301505 Share on other sites More sharing options...
AGuyWithAthing Posted December 27, 2011 Share Posted December 27, 2011 Do you have an example of your code not working? I am not sure what you mean by "My issue is when i refresh it cancels my session!" as <?php $is_ajax = $_REQUEST['is_ajax']; if(isset($is_ajax) && $is_ajax) { $username = $_REQUEST['username']; $password = $_REQUEST['password']; if($username == 'demo' && $password == 'demo') { echo "success"; } } ?> Doesn't store a session and there is no initial ajax request fired off to see if there is a session active. You would have to modify the javascript to look something like: <script type="text/javascript"> var ajaxPage = 'doLogin.php'; function checkLoggedIn() { $.ajax({ url: ajaxPage + '?event=checkLogin', success: function(response) { if( 1 == response ) $("#form1").slideUp('slow', function() { $("#message").html("<p class='success'>You have logged in successfully!</p>"); }); } }); } $(document).ready(function() { checkLoggedIn(); // Fire off a check to see if this user is logged in $("#login").click(function() { var form_data = { username: $("#username").val(), password: $("#password").val(), is_ajax: 1 }; $.ajax({ type: "POST", url: ajaxPage + '?event=doLogin', data: form_data, success: function(response) { if( 1 == response ) $("#form1").slideUp('slow', function() { $("#message").html("<p class='success'>You have logged in successfully!</p>"); }); else $("#message").html("<p class='error'>Invalid username and/or password.</p>"); } }); return false; }); }); Change the doLogin.php to something along the lines of : <?php session_start(); switch( $_GET[ 'event' ] ) { case 'checkLogin' : echo ( 1 == $_SESSION[ 'active' ] ) ? 1: 0; break; case 'doLogin' : if( isset( $_POST['is_ajax'] ) && $_POST['is_ajax'] ) { if( 'demo' == $_POST['username'] && 'demo' == $_POST['password'] ) { session_register( 'active', 1 ); echo 1; } } break; } That should work. Note: I haven't tested this so I have no idea if there are syntax or logical errors. Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301509 Share on other sites More sharing options...
hackalive Posted December 28, 2011 Author Share Posted December 28, 2011 @AGuyWithAthing You code did not work at all Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301732 Share on other sites More sharing options...
trq Posted December 28, 2011 Share Posted December 28, 2011 @AGuyWithAthing You code did not work at all So why not post yours if you want help? Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301735 Share on other sites More sharing options...
hackalive Posted December 28, 2011 Author Share Posted December 28, 2011 So while thorpe was off moving my post, I managed to isolate where the issue is comming from: It comes from this function checkLoggedIn() { $.ajax({ url: ajaxPage + '?event=checkLogin', success: function(response) { if(response == 'active') }); } If you delete all those lines (so that function, be it my modification or the exact of what I was given, it then works (but not the session part obviouslt, it just means when i try to login it does not fail.) So if anyone knows why this maybe the case, please do let me know. Cheers Ill see if i can figure it out as well Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301738 Share on other sites More sharing options...
hackalive Posted December 28, 2011 Author Share Posted December 28, 2011 I seem to have it all working .... except a logout method, how to kill all the sessions etc? I tried a PHP file which was session_start(); session_destroy(); $_SESSION['active'] = false; and run it, but it still logged me in on refresh Quote Link to comment https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.