Link to home
Start Free TrialLog in
Avatar of designersx
designersx

asked on

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

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

Avatar of designersx
designersx

ASKER

sir see the lines where i have implemented the session.
see 3,18,47,55,56,
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.
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

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.
>>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.
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.
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 :)
ASKER CERTIFIED SOLUTION
Avatar of profya
profya
Flag of Sudan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.