slee
01-17-2006, 11:16 PM
ok ive made a script for login. i cant get it to work though.
ive managed to get it so i can login to the first page but im having trouble with the session. when i go to another page it asks me to login in again :(
here is the code:
<?
include"connect.php";
session_start(); // start session.
?>
<html>
<head>
<title>Login</title>
<head>
<body>
<?
if (!isset($_POST['username']) | !isset($_POST['password'])){
// escape from php mode.
?>
<form action="<? $_SERVER['PHP_SELF']?><? if($_SERVER['QUERY_STRING']){ echo"?". $_SERVER['QUERY_STRING'];}?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
<th>
Username:
</th>
<th>
<input type="text" name="username">
</th>
</tr>
<tr>
<th>
Password:
</th>
<th>
<input type="password" name="password">
</th>
</tr>
<tr>
<th colspan="2" align="right">
<input type="submit" value="Login">
</form>
</th>
</tr>
</table>
</body>
</html>
<?
exit();
}
// If all is well so far.
$username = $_POST['username'];
$password = $_POST['password'];
//session_register("username");
//session_register("password"); // register username and password as session variables.
// Here you would check the supplied username and password against your database to see if they exist.
$sql = "SELECT password FROM Users WHERE Username = '$username' AND Password = '$password'";
$fetch_em = odbc_exec($sqlconnect, $sql);
$numrows = odbc_fetch_row($fetch_em);
if($numrows != "0") {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$valid_user = 1;
}
else {
$valid_user = 0;
}
// If the username exists and pass is correct, don't pop up the login code again.
// If info can't be found or verified....
if (!($valid_user))
{
session_unset(); // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
?>
<form action="<? $_SERVER['PHP_SELF']?><? if($_SERVER['QUERY_STRING']){ echo"?". $_SERVER['QUERY_STRING'];}?>" method="POST">
<p align="center">Incorrect login information, please try again. You must login to access this document.</p>
<table align="center" border="0">
<tr>
<th>
Username:
</th>
<th>
<input type="text" name="username">
</th>
</tr>
<tr>
<th>
Password:
</th>
<th>
<input type="password" name="password">
</th>
</tr>
<tr>
<th colspan="2" align="right">
<input type="submit" value="Login">
</form>
</th>
</tr>
</table>
</body>
</html>
<?
exit();
}
?>
where am i going wrong?
ive managed to get it so i can login to the first page but im having trouble with the session. when i go to another page it asks me to login in again :(
here is the code:
<?
include"connect.php";
session_start(); // start session.
?>
<html>
<head>
<title>Login</title>
<head>
<body>
<?
if (!isset($_POST['username']) | !isset($_POST['password'])){
// escape from php mode.
?>
<form action="<? $_SERVER['PHP_SELF']?><? if($_SERVER['QUERY_STRING']){ echo"?". $_SERVER['QUERY_STRING'];}?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
<th>
Username:
</th>
<th>
<input type="text" name="username">
</th>
</tr>
<tr>
<th>
Password:
</th>
<th>
<input type="password" name="password">
</th>
</tr>
<tr>
<th colspan="2" align="right">
<input type="submit" value="Login">
</form>
</th>
</tr>
</table>
</body>
</html>
<?
exit();
}
// If all is well so far.
$username = $_POST['username'];
$password = $_POST['password'];
//session_register("username");
//session_register("password"); // register username and password as session variables.
// Here you would check the supplied username and password against your database to see if they exist.
$sql = "SELECT password FROM Users WHERE Username = '$username' AND Password = '$password'";
$fetch_em = odbc_exec($sqlconnect, $sql);
$numrows = odbc_fetch_row($fetch_em);
if($numrows != "0") {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$valid_user = 1;
}
else {
$valid_user = 0;
}
// If the username exists and pass is correct, don't pop up the login code again.
// If info can't be found or verified....
if (!($valid_user))
{
session_unset(); // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
?>
<form action="<? $_SERVER['PHP_SELF']?><? if($_SERVER['QUERY_STRING']){ echo"?". $_SERVER['QUERY_STRING'];}?>" method="POST">
<p align="center">Incorrect login information, please try again. You must login to access this document.</p>
<table align="center" border="0">
<tr>
<th>
Username:
</th>
<th>
<input type="text" name="username">
</th>
</tr>
<tr>
<th>
Password:
</th>
<th>
<input type="password" name="password">
</th>
</tr>
<tr>
<th colspan="2" align="right">
<input type="submit" value="Login">
</form>
</th>
</tr>
</table>
</body>
</html>
<?
exit();
}
?>
where am i going wrong?
