I am new to PHP programming. A good programming friend and I have created a site using PHP and MySQL. We have decided to use sessions to allow registered users the ability to log in to the site using a login name and password. (see portion of script below). The script works fine in that it allows users to login and, when they log out, they cannot use the back button to get back into the site (as it redirects them to the login page). What we are trying to do is to get the session to expire after, say, fifteen minutes of non-use (if the user logs in and then walks away from their computer for an extended amount of time, the session will automatically expire and will redirect the user to the login page...and prevents anyone else from jumping on the user's computer and monkey-ing with the secure portion of the site.) My code is below. What else do we need to add in order to get the session to expire after 15 minutes?
Thanks,
Greg
<?php
/* Program: Login.php
Desc: Login program for the Members Only section of the
referral form. It provides two options: (1) login using an
existing Login Name and (2) enter a new login name. Login
Names and passwords are stored in a MySQL database.
*/
session_start();
session_register('$_SESSION[auth]'); // 9
session_register('$_SESSION[logname]');
session_register('$_SESSION[ID]');
session_register('$_SESSION[ID_agency]');
include("functions.php"
;
include("cup.inc"
;
switch (@$do) // 12
{
case "login": // 14
$connection = mysql_connect($host, $user,$password) // 15
or die ("Couldn't connect to server."
;
$db = mysql_select_db($database, $connection)
or die ("Couldn't select database."
;
$sql = "SELECT un FROM agents WHERE un='$fusername'";
//echo $sql;
$result = mysql_query($sql)
or die("Couldn't execute query."
;
$num = mysql_num_rows($result); // 23
if ($num == 1) // login name was found 24
{
$sql = "SELECT un, c_name, ID FROM agents WHERE un='$fusername' AND pw=password('$fpassword')";
$result2 = mysql_query($sql)
or die("Couldn't execute the query."
;
$num2 = mysql_num_rows($result2);
if ($num2 > 0) // password is correct
{
// 32
$_SESSION[auth]="yes"; // 34
$_SESSION[logname]=$fusername;
$today = date("Y-m-d h:m:s"
; // 36
$row=mysql_fetch_array($result2);
$_SESSION[ID_agency]=$row[ID]; // 35
$sql = "INSERT INTO login (un,logintime)
VALUES ('$_SESSION[logname]','$today')";
mysql_query($sql) or die("Can't execute this query."
;
if ($row[c_name]){
header("Location: agent_form.php"
;
}
else {
$_SESSION[ID]=$row[ID];
header("Location: register.php"
;
}
}
else // password is not correct // 42
{
unset($do); // 44
$message="The Login Name, '$fusername' exists,<br>but you have not entered the correct password!<br>Please try again.<br>";
include("login_form.inc"
; // 48
}
} // 50
elseif ($num == 0) // login name not found // 51
{
unset($do); // 53
$message = "The Login Name you entered does not
exist!<br>Please try again.<br>";
include("login_form.inc"
;
}
break;
Thanks,
Greg
<?php
/* Program: Login.php
Desc: Login program for the Members Only section of the
referral form. It provides two options: (1) login using an
existing Login Name and (2) enter a new login name. Login
Names and passwords are stored in a MySQL database.
*/
session_start();
session_register('$_SESSION[auth]'); // 9
session_register('$_SESSION[logname]');
session_register('$_SESSION[ID]');
session_register('$_SESSION[ID_agency]');
include("functions.php"

include("cup.inc"

switch (@$do) // 12
{
case "login": // 14
$connection = mysql_connect($host, $user,$password) // 15
or die ("Couldn't connect to server."

$db = mysql_select_db($database, $connection)
or die ("Couldn't select database."

$sql = "SELECT un FROM agents WHERE un='$fusername'";
//echo $sql;
$result = mysql_query($sql)
or die("Couldn't execute query."

$num = mysql_num_rows($result); // 23
if ($num == 1) // login name was found 24
{
$sql = "SELECT un, c_name, ID FROM agents WHERE un='$fusername' AND pw=password('$fpassword')";
$result2 = mysql_query($sql)
or die("Couldn't execute the query."

$num2 = mysql_num_rows($result2);
if ($num2 > 0) // password is correct
{
// 32
$_SESSION[auth]="yes"; // 34
$_SESSION[logname]=$fusername;
$today = date("Y-m-d h:m:s"

$row=mysql_fetch_array($result2);
$_SESSION[ID_agency]=$row[ID]; // 35
$sql = "INSERT INTO login (un,logintime)
VALUES ('$_SESSION[logname]','$today')";
mysql_query($sql) or die("Can't execute this query."

if ($row[c_name]){
header("Location: agent_form.php"

}
else {
$_SESSION[ID]=$row[ID];
header("Location: register.php"

}
}
else // password is not correct // 42
{
unset($do); // 44
$message="The Login Name, '$fusername' exists,<br>but you have not entered the correct password!<br>Please try again.<br>";
include("login_form.inc"

}
} // 50
elseif ($num == 0) // login name not found // 51
{
unset($do); // 53
$message = "The Login Name you entered does not
exist!<br>Please try again.<br>";
include("login_form.inc"

}
break;