Forum Moderators: coopster

Message Too Old, No Replies

stuck with sessions!

         

ayushchd

12:36 pm on May 5, 2007 (gmt 0)

10+ Year Member



<?
// Use session variable on this page. This function must put on the top of page.
session_start();

if (isset($_COOKIE['user'])) {

if (session_is_registered("password")) {

header ("location: main.php");

} }
/////////////////////////////////////////////////////////////////////////////////////////////

if (!isset($_COOKIE['user'])) {

if (!session_is_registered("password")) {

$message="";

////// Login Section.
$Login=$_POST['Login'];
if($Login){ // If clicked on Login button.
$username=$_POST['username'];
$password=md5($_POST['password']); // Encrypt password with md5() function.

// Connect database.
$host="sql1.example.com"; // Host name.
$db_user="user"; // MySQL username.
$db_password="pass"; // MySQL password.
$database="database"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("password");
setcookie("user", $username, $time+3600);
header("location:main.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message="--- Incorrect Username or Password ---";
}

} // End Login authorize check.
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="css.css">
<title>Untitled Document</title>
</head>

<body>
<? echo $message;?>
<div class = "normal" align="center">
<form id="form1" name="form1" method="post" action="">
<table>
<tr>
<td>User : </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>
</table>
<input name="Login" type="submit" id="Login" value="Login" />
</form>
</body>
</html>

<?

}
} else if (isset($_COOKIE['user'])) {

if (!session_is_registered("password")) {

$message="";

////// Login Section.
$Login=$_POST['Login'];
if($Login){ // If clicked on Login button.
$username=$_POST['username'];
$password=md5($_POST['password']); // Encrypt password with md5() function.

// Connect database.
$host="sql1.example.com"; // Host name.
$db_user="user"; // MySQL username.
$db_password="pass"; // MySQL password.
$database="database"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Check matching of username and password.
$result=mysql_query("select * from users where username='" .$_COOKIE['user']."' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("password");
header("location:main.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message="--- Incorrect Username or Password ---";
}

}// End Login authorize check.
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="css.css">
<title>Untitled Document</title>
</head>

<body>
<? echo $message;?>
<div class = "normal" align="center">
<form id="form1" name="form1" method="post" action="">
<table>
<tr>
<td>User : </td>
<td><?= $_COOKIE['user'];?></td>
</tr>
<tr>
<td>Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
</table>
<input name="Login" type="submit" id="Login" value="Login" />
</form>
</body>
</html>
<? } }?>

When will the session expire? I heard that the session expires if the browser's window is closed...but it doesn't happen...

[edited by: dreamcatcher at 8:53 pm (utc) on May 5, 2007]
[edit reason] Removed specifics. [/edit]

henry0

1:06 pm on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are still using session register
check first how stands REGISTER GLOBAL in your sever environment