Jump to content

Recommended Posts

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."

Link to comment
https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301380
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301509
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/253844-ajax-with-php-login/#findComment-1301738
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.