Jump to content

[SOLVED] Class not available in ajax post


DjMikeS

Recommended Posts

Hi all,

 

I have been breaking my head over this one and I can't seem to figure it out...

 

The problem:

I have an AJAX login form which posts the data (username and password) to checklogin.php.

Checklogin.php then includes some files to do the actual checking and returns a value indicating the status.

Now I have a class (register) which I use to set variables that need to be available throughout the website.

However, when I try to set a variable ( $registry->set ('testvar', 'testval'); ) I get the following error:

 

Fatal error: Call to a member function set() on a non-object in /var/www/dev.phison.nl/test/users/functions/user_login.php on line 17

 

Here is the relevant code:

Main index.php:

<?php
require 'main_includes/init.php';
require $site_path . 'main_classes/databuffer.pclass';

if (!$registry['loginstatus']) {
$registry->set ('action', 'login');
$registry->set ('section', 'users');
}
require $site_path . 'main_includes/header.php';

if ($registry['section']) {
require $site_path . $registry['section'] . "/index.php";
}
?>
<body>
<div class="global">
<div class="header"></div>
<?php echo $cntBody; ?>

</div>
</body>

 

The header which includes the javascript file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" />
<script src="jscript/jquery.js" type="text/javascript" language="javascript"></script>
<?php
if ($registry['section']) {
echo "<link href='" . $registry['section'] . "/css/style.css' rel='stylesheet'type='text/css' />\n
	  <script src='" . $registry['section'] . "/jscript/" . $registry['action'] . ".js' type='text/javascript' language='javascript'></script>\n";
}
echo "<title>:: Phison Admin Panel 2K - " . $registry['action'] . "</title>";
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if lte IE 7]>  
<link href="css/style-ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>

 

The jscript file:

$(document).ready(function()
{
$("#login_form").submit(function() {
	$("#msgbox").removeClass().addClass('check').html('<img src="../../../img/wait.gif" />').fadeIn(1000);
	var username = $("#username").val();
	var password = $("#password").val();
	$.post("users/checklogin.php",{ username: username, password: password }, function(data) {
		if(data=='0') {
			$("#msgbox").removeClass().addClass('error').html('<b>An error occurred.</b><br /><br />You haven\'t entered a username or password.');
		}
		else if(data=='1') {
			$("#msgbox").removeClass().addClass('error').html('<b>An error occurred.</b><br /><br />The username and password combination is not correct.');		
		}
		else if(data=='2') {
			$("#msgbox").removeClass().addClass('error').html('<b>An error occurred.</b><br /><br />There was an unknown error while trying to log you in. Please try again.');
		}
		else if(data=='3') {
			$("#msgbox").removeClass().addClass('loginok').html('Logging in.....');
			//redirect to secure page
			document.location='index.php';
		}
		else {
			$("#msgbox").removeClass().html('<b>An error occurred.</b><br />' + data).addClass('error');	
		}
	});
	return false; //not to post the form physically
});
});

 

The index.php in the users dir:

<?php
if (isset($_GET['cmd'])) {
switch ($_GET['cmd']) {
case 'logout' :
	logout();
	echo "<script type='text/javascript'>window.location = '../index.php'</script>";
	die();
default: 
	break;
}
}

$tplLogin = new DataBuffer();
?>
<span id="msgbox"></span>
<fieldset class="fieldset_login">
<legend align="center">Please login</legend>
<form method="post" action="" id="login_form" />
<label for="username" class="login">Username:</label><input name="username" type="text" id="username" value="" maxlength="20" class="textlogin" />
<label for="Password" class="login">Password:</label><input name="password" type="password" id="password" value="" maxlength="20" class="textlogin" /><br />
<center><input name="submit" type="submit" value="Login" class="submitlogin" id="submit" /></center>
</form>
</fieldset>

<?php
$cntBody = $tplLogin->close();
?>

 

The checklogin.php:

<?php
include ("../main_includes/db.php");
require "functions/check_credentials.php";
require "functions/check_form_empty.php";
require "functions/user_login.php";
require "functions/user_logout.php";

$username = mysql_real_escape_string($_POST['username']);
$password = sha1($_POST['password']);
$checkFormEmpty = checkFormEmpty($_POST['username'], $_POST['password']);
if (!$checkFormEmpty) {
echo "0";
}
elseif ($checkFormEmpty) {
$checkCredentials = checkCredentials($username, $password);
if (!$checkCredentials) {
	echo "1";
}
elseif ($checkCredentials) {
	$userLogin = userLogin($username, $password);
	if (!$userLogin) {
		echo "2";
	}
	else {
		echo "3";
	}
}
}
?>

 

I'll only show check_form_empty.php here, because the rest of the function scripts return the same:

<?php
function checkFormEmpty($username, $password) {
if (empty($username)) {
	return false;
}
elseif (empty($password)) {
	return false;
}
else {
	return true;
}
}
?>

 

I think that the ajax post in the jscript file calls the checklogin.php outside of the environment so that eg. the registry class is not available.

 

Is there a way to solve this without including the registry.pclass file in the checklogin script ?

 

If you want to check it out: https://dev.phison.nl/test/

And I know it uses an invalid security certificate ;) I still need to change that....  ::)

 

Thanks in advance.

Link to comment
Share on other sites

Web servers are stateless (when the request for a page is completed all resources used on that page are destroyed) and each request is completely separate, including requests made by AJAX.

 

Any page requested is its own separate island. Nothing is shared or persists on the server between pages or requests for the same page.

Link to comment
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.