Forum Moderators: coopster

Message Too Old, No Replies

cookies and session.together?

         

ayushchd

11:06 am on May 5, 2007 (gmt 0)

10+ Year Member



Hi All This Is My Code For The Login Page:

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

////// Logout Section. Delete all session variable.
session_destroy();

$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.phpnet.us"; // Host name.
$db_user="paaaaa6"; // MySQL username.
$db_password="aaaaaaasdh"; // MySQL password.
$database="pn_334856_banks"; // 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">
<title>Untitled Document</title>
</head>

<body>
<? echo $message;?>
<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>

After I enter the correct username and password, the page refreshes itself instead of heading to the main.php.....Dunno wats the problem?

henry0

11:43 am on May 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The form function as “SELF”
Many among us will prefer using a separated page for the form boby and the exec script
However I believe you “kill” it from the very top
You start a SESSION then DESTROY it
Try commenting the session destroy to check it out
Then if it works just create a small script snippet that will be your logout

You should not use session_register() any longer
By definition a $_SESSION is registered so if the session exists then it is registered
Most certainly REGISTER GLOBAL is OFF in your server environment

Fom the manual:

Caution
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

MANUAL [us2.php.net]