Solved

why does not the page stays at wher eit is when i  refresh the page even when i am using the session correctly.

Posted on 2009-07-09
9
169 Views
Last Modified: 2012-05-07
below is my ajax login form. after login , admin link is shown, on clicking that it shows i am in dropdown list page, this is private page. now when i refresh the page then it redirects me to the login page. why does not the page remains/ stays there? why??

i have 3 functions login(), logout(), dropdownlist(). the code is otherwise working. the only problem is that on refreshing the page, why does not that page stays there, why it tries to go index page.


config.php //database connection is in it and session is started.

<?php

	session_start();

	$db_host="localhost";

	$db_name="session_cms_ajax";

	$username="root";

	$password="";

	$db_con=mysql_connect($db_host,$username,$password) or die("connection not build".mysql_error());

	$db=mysql_select_db($db_name) or die("database not build".mysql_error());

?>
 

index.php   //login form is shown

<?php include('config.php'); ?>

<script type="text/javascript" language="javascript" src="javascript/login.js"></script>
 

<div id="login" style="background-color:#CCCCCC;"></div> 

<br />

<?php if(!isset($_SESSION['loggedin'])){

?>

<div id="loginform">

<form action="javascript:login()" method="post">

<table border="1" bgcolor="#CCCCCC">

	<tr><td colspan="2" align="center"><h3>Login Form</h3></td></tr>

	<tr><td>Username</td><td><input name="username" type="text" id="username" /></td></tr>

	<tr><td>Password</td><td><input name="password" type="password" id="password" /></td></tr>

	<tr><td>&nbsp;</td><td><input type="button" name="login" value="Login" onclick="mylogin();" />

	<input type="button" name="logout" value="Logout" onclick="mylogout();" /></td>

	</tr>

</table>

</form>

</div>

<?php }?>

<div id="dropdownlist"></div>

<div id="logout"></div>
 

//login.php

<?php 

include('config.php'); 

$username = $_GET['username'];

$password = $_GET['password'];
 

$sql = 'select count(*) as total from login where username="'. $username . '" and password = "' . $password . '"';

$query = mysql_query($sql);

$rec = mysql_fetch_assoc($query);

$num_rows = intval($rec['total']);

if( $num_rows > 0){

	$_SESSION['loggedin'] = 1 ;

	?> <div id="login"><a href="javascript:dropdownlist();"><h3><?php echo $username; ?></h3></a></div> <?php 

}

else{ echo "no"; }

?>
 

dropdownlist.php

<?php include('config.php'); 

if(isset($_SESSION['loggedin'])){

	echo $_SESSION['loggedin'];

	echo "i am in dropdownlist page. This is my private page.";

}

?>
 

logout.php

<?php

include('config.php');

session_destroy();

$_SESSION['loggedin'] = 0 ;

?>
 

login.js

function createObject() {

	var request_type;

	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer"){

		request_type = new ActiveXObject("Microsoft.XMLHTTP");

	} else{

		request_type = new XMLHttpRequest();

	}

	return request_type;

}

var http = createObject();
 

function mylogin() {

	var username = encodeURI(document.getElementById('username').value);

	var password = encodeURI(document.getElementById('password').value);

	http.open('get', 'login.php?username='+username+'&password='+password);

	http.onreadystatechange = function(){

	 if(http.readyState == 4){

		var response = http.responseText;

		var res = response.replace(/^\s+|\s+$/, '');

		if(res == "no"){

				document.getElementById('login').innerHTML = 'Login Failed';

				document.getElementById('loginform').style.display = 'block';

		} else {

				document.getElementById('login').innerHTML = response;

				document.getElementById('loginform').style.display = 'none';

		}

	}

	}

	http.send(null);

}
 

function mylogout(){

	document.getElementById('loginform').style.display = 'none';

	http.open('get','logout.php', true); // here we are calling the respective page

	http.onreadystatechange = function(){

	if(http.readyState == 4){

		var response = http.responseText;

		//alert(response)

		document.getElementById('logout').innerHTML = response; // menu links

		}

	}

	http.send(null);		

}
 

function dropdownlist(){

	http.open('get','dropdownlist.php', true); // here we are calling the respective page

	http.onreadystatechange = function(){

	if(http.readyState == 4){

		var response = http.responseText;

		//alert(response)

		document.getElementById('dropdownlist').innerHTML = response; // menu links

		}

	}

	http.send(null);

}

Open in new window

0
Comment
Question by:designersx
  • 5
  • 4
9 Comments
 

Author Comment

by:designersx
Comment Utility
sir see the lines where i have implemented the session.
see 3,18,47,55,56,
0
 
LVL 14

Expert Comment

by:profya
Comment Utility
This happens because when you refresh you actually refresh the index page. The logic in the index page is if the user is not logged on then ask him/her for user name and password, other wise, do nothing. So when you log in for the first time and then refresh the page nothing is printed. All stuff you do by ajax are runtime actions, when you reload the page it disappears, because it is contained in divs.
0
 
LVL 14

Expert Comment

by:profya
Comment Utility
To test this add this to the index page:
else
{
      echo "Welcome, you are already logged in";
}?>

<?php include('config.php'); ?>

<script type="text/javascript" language="javascript" src="javascript/login.js"></script>
 

<div id="login" style="background-color:#CCCCCC;"></div>

<br />

<?php if(!isset($_SESSION['loggedin'])){

?>

<div id="loginform">

<form action="javascript:login()" method="post">

<table border="1" bgcolor="#CCCCCC">

        <tr><td colspan="2" align="center"><h3>Login Form</h3></td></tr>

        <tr><td>Username</td><td><input name="username" type="text" id="username" /></td></tr>

        <tr><td>Password</td><td><input name="password" type="password" id="password" /></td></tr>

        <tr><td> </td><td><input type="button" name="login" value="Login" onclick="mylogin();" />

        <input type="button" name="logout" value="Logout" onclick="mylogout();" /></td>

        </tr>

</table>

</form>

</div>

<?php }

else

{

	echo "Welcome, you are already logged in";

}?>

<div id="dropdownlist"></div>

<div id="logout"></div>

<a href="logout.php">logout</a>

Open in new window

0
 
LVL 14

Expert Comment

by:profya
Comment Utility
It seems to me it is just an example with which you are demonstrating using ajax in login pages, however, to preserve the layout of the logged on user page, you may need to change the whole logic. You use ajax only to perform the authentication and hide the div that contains the form, is login is successful.
I hope this helps.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:designersx
Comment Utility
>>This happens because when you refresh you actually refresh the index page.

you are exactly 200% right. this answer i wanted to know why it happens.
ur second last post is so valuable i can't tell u.
thanks for that.

ok now code, yes it will show me the message welcome if already logged in. i m agree with you.

can't we do it anything so that the content which is displayed to us gets displayed to us.
0
 

Author Comment

by:designersx
Comment Utility
check out this link.

http://dev.wringstudio.com/login/

username and password is

admin, admin

when u login and go inside, and then refresh that page it will show you the login page. i understand why. i will post all the code of this  but could we be able to show what is being displayed.
0
 
LVL 14

Expert Comment

by:profya
Comment Utility
The login needs to change in the case, for example we may need to cash the username in the session, and when the user is already logged on, we can then echo the username link that points to the doropdownlist javascript function, it will then take care of the rest. Or you can change the whole scenario, it is a matter of logic, the tools you require are there and you know how to use them. I liked the ajax authentication idea by the way, it saves time if there is a problem logging on, no need to postbacks, however, if I were you I'll redirect the user to the control panel page when authentication is successful and let him/her enjoy using the system normally.
I hope this helps :)
0
 
LVL 14

Accepted Solution

by:
profya earned 250 total points
Comment Utility
I'll try to answer it there.
0
 

Author Comment

by:designersx
Comment Utility
1) firstly, thanks for all of ur very valuable posts.  you seem to be best among i have encountered yet. real thanks for ur great help.

2)the concept of hiding divs and showing divs is right or not in real terms. i am a beginner in ajax, so i tried with this. AM I RIGHT? Is there any other better method u know?

3)>>however, if I were you I'll redirect the user to the control panel page when authentication is successful and let him/her enjoy using the system normally.

i m not able to understand by redirecting the user to  control panel.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now