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
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 

Author Comment

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

Expert Comment

by:profya
ID: 24816499
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
ID: 24816518
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:profya
ID: 24816578
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
 

Author Comment

by:designersx
ID: 24816624
>>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
ID: 24816696
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
ID: 24816724
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
ID: 24816749
I'll try to answer it there.
0
 

Author Comment

by:designersx
ID: 24816905
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

751 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