Page 2 of 2

Posted: Mon May 22, 2006 5:25 am
by madhu
Finally after searching in Google , i got the following code.

Please check and reply ,am getting the error as

http://localhost/practice/<br%20/><b>No ... /b><br%20/>"

i created table
-------------------

Code: Select all

create table sessions ( 
id int(10) NOT NULL AUTO_INCREMENT, 
sess_key char(6) NOT NULL, 
val varchar(250) NOT NULL, 
ip varchar(35) NOT NULL, 
access int(25) NOT NULL, 
PRIMARY KEY(id) 
);

login.php:
----------

Code: Select all

"<?php

include "sess.php";

if($login) {

$sess = new session;

$sess->start();

$sess->register($username);

header("Location: welcome.php");

}

?>
<html>
<head>
<title>login</title>
</head>

<body>

<form method="post" action="<?= $PHP_SELF; ?>">

Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<br>
<input type="submit" name="login" value="Login">

</body>
</html>

welcome.php:
---------------

<?php

include "sess.php";

$sess = new session;

if($logout == "yes") {
$sess->destroy();
header(“Location: login.php”);
exit;
}

$sess->read();
?>
<html>
<head>
<title>welcome</title>
</head>

<body>

Welcome, <?= $sess_val; ?><br>
<a href="<?= $PHP_SELF; ?>?logout=yes">Logout</a>

</body>
</html>


sess.php:
------------
<?php

$host = "localhost";
$user = "root";
$pass = "";
$db = "gulfnews";

$dbc = mysql_connect($host,$user,$pass) or die("Cannot establish a connection to the database.");
mysql_select_db($db,$dbc);

class session {

var $key;
var $timeout;
}



// start() will initialize the session by generating the session key or ID
function start($timeout = "") {

// create an array with all the letters of the alphabet
$letters = range("a","z");
// declare the $key variable
$key = "";

// generate our session's key formatted such as #a#aa#
for($i = 0; $i < 6; $i++) {
if(($i == 0) || ($i == 2) || ($i == 5))
$key .= rand(0,9);
if(($i == 1) || ($i == 3) || ($i == 4))
$key .= $letters[rand(0,25)];
}

// store the session's key in a method of the class
$this->key = $key;

// perform a conditional to test if the user defined the timeout and if not store the default value.
if($timeout == "")
$this->timeout = 120; // five minutes
else
$this->timeout = $timeout;

return 0;
}


// this function will register a value to session. (only one value, see replace() to update the value)
function register($val) {

// if key is not generated run start()
if($this->key == "")
$this->start();

$insert = mysql_query("INSERT INTO sessions (sess_key, val, ip, sec_expire, stamp_expire, access) VALUES ('" . $this->key. "', '" . addslashes($val) . "' , '" . $_SERVER["REMOTE_ADDR"] . "' , " . $this->timeout . "," . (time() + $this->timeout) . "," . time() .");");
// set the cookie that will store the session key
setcookie("sess_key",$this->key,time()+3600);
}


function read() {

// set $sess_val global - the variable of the session value.
global $sess_val;

// if the cookie doesn't exisit send them back to the login screen.
if(!$_COOKIE["sess_key"]) {
header("Location: login.php");
exit;
}

// fetch the session key from the cookie.
$this->key = $_COOKIE["sess_key"];

// fetch the session value
$query = mysql_query("SELECT val FROM sessions WHERE sess_key = '" . $this->key . "'") or die("query failed - line 55");

if(mysql_num_rows($query) == 0) {
header("Location: login.php");
exit;
}

$fetch = mysql_fetch_array($query);

// store the session value to $sess_val
$sess_val = stripslashes($fetch["val"]);

// test if session has reached the expiration point
$this->expire();

// this code will only run if expire() returned falsed - we update the last access point to now.
$update = mysql_query("UPDATE sessions SET access = " . time() . " WHERE sess_key = '" . $this->key . "'") or die("query failed - line 70");

}


// this function will test if the user has been inactive for the defined timeout
function expire() {

// fetch the last access and expirations from the database
$query = mysql_query("SELECT access, sec_expire, stamp_expire FROM sessions WHERE sess_key = '" . $this->key . "'") or die("query failed - line 78");
$fetch = mysql_fetch_array($query);

$access = $fetch["access"];
$expire = $fetch["sec_expire"];
$timeout = $fetch["stamp_expire"];

// test if session is expired based on defined timeout
if(($timeout - $access) <= ($expire - $expire)) {
$this->destory();
die("Your session has expired. Please re-login.");
}
}



// this function will update the session value
function replace($val) {

// fetch the user key from cookie
$this->key = $_COOKIE["sess_key"];

// update the database with the new value
$query = mysql_query("UPDATE sessions SET val = '" . $val . "' WHERE sess_key = '" . $this->key) or die("query failed - line 77");
}

//Our function asks for the new value and then it will update the current value with it. Finally, we reach our last function:

// this function will kill the session
function destroy($key = "") {

// fetch the user key from cookie
$this->key = $_COOKIE["sess_key"];

// delete session from database
$query = mysql_query("DELETE FROM sessions WHERE sess_key = '" . $this->key . "'") or die("query failed - line 86");

// remove cookie from the user's computer
$delete = setcookie("sess_key" , $this->key, time()-3600);

if($query && $delete) {
header("Location login.php");
exit;
}
}
?>
kindly please see the code and please help me what is wrong in this code...

waiting for your valuable replies...........

thanks and regards
madhu

Posted: Mon May 22, 2006 5:45 am
by xpgeek
madhu wrote:skip
register_globals

Posted: Mon May 22, 2006 5:58 am
by madhu
xpgeek , can you please explain clearly what did you mean................

waiting for your reply...............

Posted: Mon May 22, 2006 8:52 am
by RobertGonzalez
madhu wrote:so if i want to redirect from page2.php to page1.php after 1 minute, it will not posible using ini_set('session.gc_maxlifetime', "60"); , is it correct??

please suggest another ways how to implement this feature......

waiting for your valuable replies.........................
This is correct. I said that earlier in this thread. If you want to redirect exactly at 10 minutes of inactivity, you are going to have to go client-side using AJAX (or even simple Javascript). It can be done as I have seen it done. But not with a server-side language. It would need to be done on the client.

Posted: Mon May 22, 2006 11:34 pm
by madhu
hi this is madhu

finally i got the output.

Firstofall thanks for your valuable replies...............

I tried in PHP using ini_set('session.cache_expire',1); , but it didnot work.

So based on Everah suggestion , i implemented using Javascript setTimeout function.

Now it is working perfectly i.e if the machine is idle for 10 seconds ,then it is redirecting to another page.

Thank you somuch for your valuable replies....................

Am sending my code , it maybe useful for someother people who want to implement this feature.

code:(if system is idle for 10 seconds ,then it will redirect to target.php)
-------------------------------------------------------------------------------------------

Code: Select all

<html>

<head>

<script language="JavaScript">
if (document.layers) {
  window.captureEvents(Event.MOUSEMOVE);
}

window.onMouseMove = resetTimer;

var tID = '';

function resetTimer(e) {
  //alert('here');
  clearTimeout(tID); // reset the timer
  tID = setTimeout('executeTimer()',10000);
}

function executeTimer() {
  location.href = 'target.php';
}
</script>

</head>

<body onLoad="tID = setTimeout('executeTimer()',10000)" onMouseMove="resetTimer()">

</body>

</html>
Thanks and regards
madhu

Posted: Tue May 23, 2006 12:18 am
by RobertGonzalez

Code: Select all

<html>
<head>
<script language="JavaScript">
if (document.layers) {
  window.captureEvents(Event.MOUSEMOVE);
}

window.onMouseMove = resetTimer;

var tID = '';

function resetTimer(e) {
  //alert('here');
  clearTimeout(tID); // reset the timer
  tID = setTimeout('executeTimer()',10000);
}

function executeTimer() {
  location.href = 'target.php';
}
</script>
</head>
<body onLoad="tID = setTimeout('executeTimer()',10000)" onMouseMove="resetTimer()">

</body>
</html>
Wrapped in

Code: Select all

 tags so others can see it the way it should look. :wink:

Posted: Tue May 23, 2006 12:49 am
by madhu
hi again one more problem came....

please help me....

if i execute that code what i sent before , for single page it is working fine.

But in project i have 4 frames like (left frame, header frame , single view frame , navigation frame ).

if i integrate that code to all 4 frames , then after 10 seconds, it is redirecting to target.php from all four frames.

please guide me how to integrate that code to all 4 frames..

Waiting for your valuable replies..........

Thanks and regards
madhu

Posted: Tue May 23, 2006 1:49 am
by RobertGonzalez
You might to search for frame names and such in google. I think it would have something to do with the location.href part of the script and the name of the window/frame that you want to redirect.

Posted: Tue May 23, 2006 2:05 am
by madhu
hi all, please check my code and please tell how to implement in frames.

here is my code:
--------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
self.resizeTo(screen.availWidth,screen.availHeight);
self.moveTo(0,0);
</script>

<script language="JavaScript">
if (document.layers) {
window.captureEvents(Event.MOUSEMOVE);
}

window.onMouseMove = resetTimer;

var tID = '';

function resetTimer(e) {
clearTimeout(tID); // reset the timer
tID = setTimeout('executeTimer()',1000);
}

function executeTimer() {
location.href = 'http://target.php';
}
</script>

/* FOUR FRAMES */

<frameset rows="63,26,*" frameborder="NO" border="0" framespacing="0">
<?php
print("<frame src=svww_header.php name=\"topFrame\" scrolling=no noresize>");
print("<frame name=navigation src=\"svww_navigation.php\" scrolling=no>");
print("<frameset cols=\"120,1*\"><frame name=leftframe src=\"svww_left.php\">");
print("<frame src=svww_singleview.php?page=0&mode=$i_mode name=\"main\" scrolling=yes noresize>");
?>
</frameset>

</head>

<body onLoad="tID = setTimeout('executeTimer()',1000)" onMouseMove="resetTimer()">
</body>

</html>

please check the code and give reply AsSoonAsPosible.............

Waiting for your valuable replies..................

Thanks and regards
madhu

Posted: Tue May 23, 2006 2:08 am
by RobertGonzalez
Can you please wrap your code in the appropriate BBCode tags? It makes it a lot easier to read when it is formatted properly. Thanks.

Posted: Tue May 23, 2006 2:22 am
by madhu
hi all,here am sending my code again by wraping ,please check and reply what is problem in code.

waiting for your valuable replies....................

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
self.resizeTo(screen.availWidth,screen.availHeight);
self.moveTo(0,0);
</script>

<script language="JavaScript">
if (document.layers) {
window.captureEvents(Event.MOUSEMOVE);
}

window.onMouseMove = resetTimer;

var tID = '';

function resetTimer(e) {
clearTimeout(tID); // reset the timer
tID = setTimeout('executeTimer()',1000);
}

function executeTimer() {
location.href = 'http://target.php';
}
</script>
/* FOUR FRAMES */

<frameset rows="63,26,*" frameborder="NO" border="0" framespacing="0">
<?php
print("<frame src=svww_header.php name=\"topFrame\" scrolling=no noresize>");
print("<frame name=navigation src=\"svww_navigation.php\" scrolling=no>");
print("<frameset cols=\"120,1*\"><frame name=leftframe src=\"svww_left.php\">");
print("<frame src=svww_singleview.php?page=0&mode=$i_mode name=\"main\" scrolling=yes noresize>");
?>
</frameset>

</head>

<body onLoad="tID = setTimeout('executeTimer()',1000)" onMouseMove="resetTimer()">
</body>

</html>

Thanks and regards
madhu

Posted: Tue May 23, 2006 9:02 am
by RobertGonzalez
What I meant was, when posting, highlight anything that is code, and at the top of the posting form, click either the

Code: Select all

button or select a code type from the "Syntax..." drop down box. In this case I am wrapping the code in [syntaxt="php'] code because you have PHP code in it...

[syntax="php"]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
	self.resizeTo(screen.availWidth,screen.availHeight);
    self.moveTo(0,0);
</script>

<script language="JavaScript">
if (document.layers) {
  window.captureEvents(Event.MOUSEMOVE);
}

window.onMouseMove = resetTimer;

var tID = '';

function resetTimer(e) {
  clearTimeout(tID); // reset the timer
  tID = setTimeout('executeTimer()',1000);
}

function executeTimer() {
  location.href = 'http://target.php';
}
</script>
/* FOUR FRAMES */

 <frameset rows="63,26,*" frameborder="NO" border="0" framespacing="0">
  <?php
	print("<frame src=svww_header.php name=\"topFrame\" scrolling=no noresize>");
	print("<frame name=navigation src=\"svww_navigation.php\" scrolling=no>");
	print("<frameset cols=\"120,1*\"><frame name=leftframe src=\"svww_left.php\">");
	print("<frame src=svww_singleview.php?page=0&mode=$i_mode name=\"main\" scrolling=yes noresize>");
  ?>
 </frameset>
</head>

<body onLoad="tID = setTimeout('executeTimer()',1000)" onMouseMove="resetTimer()">
</body>
</html>
As a side note, I thought frames were supposed to be within the <body></body> tags? Also, there is not place in that code above where the PHP var $i_mode is set. Did you mean for that to happen?[/syntax]