I have a login status indicator div on my ajax-php database's home page. The login button when clicked does two things.
<input value="Login" type="button" onclick='logIntheUser(/*function to process login request*/);updateLoginStatusIndicator(/*function to update login status in login staus indicator div*/);' />
- It calls a
logIntheUser
function to send the login data to the php access processor file to set$_SESSION['username']
if user name and password are correct.- It also calls
updateLoginStatusIndicator
function which displays logged in as$_SESSION['username']
if$_SESSION['username']
is set or show "not logged in" if$_SESSION['username']
is not set.
Here is the controller code which is executed with updateLoginStatusIndicator
function:
/*===login status indicator/updater===*/
if(isset($_REQUEST['change-login-indicator']))
{
if(!isset($_SESSION))
{
session_start();
}
if(isset($_SESSION['username']))
{
echo 'Logged in as:'.$_SESSION['username'];
}
else
{
echo 'not logged in';
}
Now the problem is that some time the user is logged in successfully(i.e. $_SESSION['username']
is set) but still the second function updateLoginStatusIndicator
displays "not logged in". The login status indicator does not work evey time. May be if the updateLoginStatusIndicator
runs after the logIntheUser
then it will update login status properly. But I don't know how to do it. Please help!
EDIT
When I call the function as callback i.e like: logIntheUser(updateLoginStatusIndicator()), it still does not not work(may be my call back method is incorrect). Then when I add set_time_limit(1);
to the updateLoginStatusIndicator()
function script to delay it for 1 second, it starts to work but its not the best practice to delay the script. Kindly help me how can i work it ou without delay so that updateLoginStatusIndicator()
function executes only after the logIntheUser()
function has finished executing. Please help.